-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1029.cpp
68 lines (61 loc) · 1.4 KB
/
P1029.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
#include<stack>
using namespace std;
bool cmp(const vector<int> &a, const vector<int> &b) {
if (a[2]!=b[2]) return a[2]>b[2];
int ma,mb;
ma=min(a[0],a[1]);
mb=min(b[0],b[1]);
if (ma!=mb) return ma<mb;
return false;
}
class Solution {
public:
int twoCitySchedCost(vector<vector<int>>& costs) {
int diff;
for (auto &v:costs) {
diff=abs(v[0]-v[1]);
v.push_back(diff);
}
sort(costs.begin(),costs.end(),cmp);
int n,l,r,i;
n=costs.size()/2;
l=r=i=0;
int ans=0;
while (l<n || r<n) {
if (costs[i][0]<=costs[i][1]) {
if (l<n) {
ans+=costs[i][0];
l++;
}
else {
ans+=costs[i][1];
r++;
}
}
else {
if (r<n) {
ans+=costs[i][1];
r++;
}
else {
ans+=costs[i][0];
l++;
}
}
i++;
}
return ans;
}
};
static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}();