# Tianchi online programming arranges interview city (greedy)

2020-12-06 15:49:54

## 1. subject

There are N Interviewers need to be interviewed , The company arranged two cities for the interview A and B, Every interviewer has come to A The cost of the city costA And to B The cost of the city costB.

The company needs to divide the interviewers into two groups , bring total cost Minimum .

``````N It's even
2<=N<=1e5
The answer is sure to be in int Within the scope of
1<=costA,costB <=1e6
``````

explain
It is required that the number to go to A is equal to the number to go to B

`````` Example
Input : cost = [[5,4],[3,6],[1,8],[3,9]]
Output : 14
explain :  The first and the second go to B City , Go to the rest A City
``````

## 2. Problem solving

• The difference is small A City
``````class Solution {

public:
/**
* @param cost: The cost of each interviewer
* @return: The total cost of all the interviewers.
*/
int TotalCost(vector<vector<int>> &cost) {

sort(cost.begin(), cost.end(),[&](auto &a, auto &b){

return a[0]-a[1] < b[0]-b[1];//  The difference is small A City
});
int sum = 0;
for(int i = 0; i < cost.size(); ++i)
{

if(i < cost.size()/2)
sum += cost[i][0];//  The difference is small A City
else
sum += cost[i][1];
}
return sum;
}
};
``````

603ms C++

Long click or sweep code pay attention to my official account （Michael amin ）, Come on together 、 Learn together ！

https://chowdera.com/2020/12/20201206154645139w.html