#include <bits/stdc++.h>
using namespace std;
#define int long long
bool comp(pair<int,int> a, pair<int,int> b){
return a.first+a.second < b.first+b.second;
}
signed main() {
int k,n;
cin >> k >> n;
if (k == 1){
int dist = 0;
vector<int> arr;
for (int i = 0; i<n; i++){
char a1,a2;
int d1,d2;
cin >> a1 >> d1 >> a2 >> d2;
if (a1 == a2){
dist += abs(d1-d2);
}
else{
dist++;
arr.push_back(d1);
arr.push_back(d2);
}
}
sort(arr.begin(),arr.end());
int vv = arr.size()/2;
int md = arr[vv];
for (auto u : arr){
dist += abs(md-u);
}
cout << dist << '\n';
}
else if (k == 2){
int dist = 0;
vector<pair<int,int>> arr;
for (int i = 0; i<n; i++){
char a1,a2;
int d1,d2;
cin >> a1 >> d1 >> a2 >> d2;
if (a1 == a2){
dist += abs(d1-d2);
}
else{
dist++;
pair<int,int> tp = {d1,d2};
arr.push_back(tp);
}
}
sort(arr.begin(),arr.end(),comp);
int fst[arr.size()], scd[arr.size()];
priority_queue<int> g,s;
int ls = 0;
int mr = 0;
for (int i = 0; i<arr.size(); i++){
g.push(-arr[i].first);
g.push(-arr[i].second);
mr += arr[i].first;
mr += arr[i].second;
while (g.size() > s.size()){
int temp = g.top();
g.pop();
s.push(-temp);
ls += abs(temp);
mr -= abs(temp);
}
int mid = s.top();
int one = (s.size()*mid)-ls;
int two = mr-(g.size()*mid);
fst[i] = abs(one)+abs(two);
}
while (g.size()) g.pop();
while (s.size()) s.pop();
ls = 0;
mr = 0;
for (int i = arr.size()-1; i>=0; i--){
g.push(-arr[i].first);
g.push(-arr[i].second);
mr += arr[i].first;
mr += arr[i].second;
while (g.size() > s.size()){
int temp = g.top();
g.pop();
s.push(-temp);
ls += abs(temp);
mr -= abs(temp);
}
int mid = s.top();
int one = (s.size()*mid)-ls;
int two = mr-(g.size()*mid);
scd[i] = abs(one)+abs(two);
}
int xtra = 1e18;
for (int i = 0; i<arr.size()-1; i++){
xtra = min(scd[i+1]+fst[i],xtra);
}
cout << dist+xtra << '\n';
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:58:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i<arr.size(); i++){
| ~^~~~~~~~~~~
bridge.cpp:99:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0; i<arr.size()-1; i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
484 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
488 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
488 KB |
Output is correct |
11 |
Correct |
1 ms |
480 KB |
Output is correct |
12 |
Correct |
41 ms |
3216 KB |
Output is correct |
13 |
Correct |
78 ms |
4392 KB |
Output is correct |
14 |
Correct |
53 ms |
3544 KB |
Output is correct |
15 |
Correct |
48 ms |
2660 KB |
Output is correct |
16 |
Correct |
59 ms |
3864 KB |
Output is correct |
17 |
Correct |
67 ms |
4288 KB |
Output is correct |
18 |
Correct |
65 ms |
4040 KB |
Output is correct |
19 |
Correct |
74 ms |
4280 KB |
Output is correct |
20 |
Correct |
59 ms |
3940 KB |
Output is correct |
21 |
Correct |
80 ms |
4040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
440 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |