Leetcode-1029 Two City Scheduling(两地调度)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 #define pb push_back
 3 struct k
 4 {
 5     int a;
 6     int b;
 7     int diff;
 8     bool operator < (k b)
 9     {
10         return diff<b.diff;
11     }
12 };
13 class Solution
14 {
15     public:
16         int twoCitySchedCost(vector<vector<int>>& costs)
17         {
18             vector<k> v;
19             _for(i,0,costs.size())
20             {
21                 k kk;
22                 kk.a = costs[i][0];
23                 kk.b = costs[i][1];
24                 kk.diff = kk.a-kk.b;
25                 v.pb(kk);
26             }
27             
28             sort(v.begin(),v.end());
29             
30             int sum = 0;
31             _for(i,0,costs.size())
32                 sum += min(costs[i][1],costs[i][0]);
33             
34             _for(i,0,costs.size())
35             {
36                 if(i < costs.size()/2 && v[i].diff>0)
37                     sum += v[i].diff;
38                 if(i >= costs.size()/2 && v[i].diff<0)
39                     sum += -v[i].diff;
40             }
41             return sum;
42         }
43 };

 

posted @ 2019-04-21 13:44  Asurudo  阅读(463)  评论(0编辑  收藏  举报