#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct ppl {
ll h,o; //house, office
bool operator < (const ppl & x) const {
return h+o < x.h+x.o;
}
};
ll n,m,k,x,y,z;
char st, ed;
ll cal(vector<ppl> & city, vector<ll> & median) {
ll ans = 0;
if (median.size()) {
sort(median.begin(),median.end());
ll bridge = median[median.size()/2];
for (auto i : city) ans += abs(i.h-bridge) + abs(i.o-bridge) + 1;
}
return ans;
}
signed main() {
cin >> m >> n;
vector<ll> median;
vector<ppl> city;
ll ans = 0, mn = 1e18;
for (int i = 1; i <= n; i++) {
cin >> st >> x >> ed >> y;
if (st == ed) ans += abs(x-y);
else city.push_back({x,y});
}
sort(city.begin(),city.end());
for (int i = 0; i <= (m == 1 ? 0 : city.size()); i++) {
vector<ppl> c1, c2;
vector<ll> m1, m2;
for (int j = 0; j < i; j++) {
c1.push_back(city[j]);
m1.push_back(city[j].h);
m1.push_back(city[j].o);
}
for (int j = i; j < city.size(); j++) {
c2.push_back(city[j]);
m2.push_back(city[j].h);
m2.push_back(city[j].o);
}
mn = min(mn,cal(c1,m1)+cal(c2,m2));
}
cout << ans + mn << "\n";
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
36 | for (int i = 0; i <= (m == 1 ? 0 : city.size()); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:44:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ppl>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int j = i; j < city.size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
53 ms |
6728 KB |
Output is correct |
13 |
Correct |
132 ms |
6748 KB |
Output is correct |
14 |
Correct |
76 ms |
6440 KB |
Output is correct |
15 |
Correct |
81 ms |
3580 KB |
Output is correct |
16 |
Correct |
84 ms |
6928 KB |
Output is correct |
17 |
Correct |
97 ms |
6848 KB |
Output is correct |
18 |
Correct |
96 ms |
6852 KB |
Output is correct |
19 |
Correct |
109 ms |
6720 KB |
Output is correct |
20 |
Correct |
85 ms |
6720 KB |
Output is correct |
21 |
Correct |
98 ms |
6804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
34 ms |
340 KB |
Output is correct |
14 |
Correct |
87 ms |
384 KB |
Output is correct |
15 |
Correct |
98 ms |
412 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
28 ms |
372 KB |
Output is correct |
18 |
Correct |
13 ms |
304 KB |
Output is correct |
19 |
Correct |
33 ms |
400 KB |
Output is correct |
20 |
Correct |
39 ms |
340 KB |
Output is correct |
21 |
Correct |
84 ms |
344 KB |
Output is correct |
22 |
Correct |
43 ms |
308 KB |
Output is correct |
23 |
Correct |
28 ms |
340 KB |
Output is correct |
24 |
Correct |
40 ms |
396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
30 ms |
340 KB |
Output is correct |
14 |
Correct |
89 ms |
404 KB |
Output is correct |
15 |
Correct |
97 ms |
408 KB |
Output is correct |
16 |
Correct |
4 ms |
344 KB |
Output is correct |
17 |
Correct |
25 ms |
340 KB |
Output is correct |
18 |
Correct |
17 ms |
344 KB |
Output is correct |
19 |
Correct |
38 ms |
396 KB |
Output is correct |
20 |
Correct |
38 ms |
340 KB |
Output is correct |
21 |
Correct |
84 ms |
504 KB |
Output is correct |
22 |
Correct |
42 ms |
396 KB |
Output is correct |
23 |
Correct |
28 ms |
308 KB |
Output is correct |
24 |
Correct |
43 ms |
340 KB |
Output is correct |
25 |
Execution timed out |
2065 ms |
8776 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |