#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);
}
}
if (arr.size() == 0){
cout << dist << '\n';
return 0;
}
sort(arr.begin(),arr.end(),comp);
int sz = arr.size();
int fst[sz], scd[sz];
priority_queue<int> g,s;
int ls = 0;
int mr = 0;
for (int i = 0; i<sz; 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 = sz-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<sz-1; i++){
xtra = min(scd[i+1]+fst[i],xtra);
}
cout << dist+xtra << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
404 KB |
Output is correct |
7 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
35 ms |
2440 KB |
Output is correct |
13 |
Correct |
78 ms |
2552 KB |
Output is correct |
14 |
Correct |
50 ms |
2500 KB |
Output is correct |
15 |
Correct |
45 ms |
1560 KB |
Output is correct |
16 |
Correct |
54 ms |
2500 KB |
Output is correct |
17 |
Correct |
69 ms |
2588 KB |
Output is correct |
18 |
Correct |
65 ms |
2500 KB |
Output is correct |
19 |
Correct |
78 ms |
2500 KB |
Output is correct |
20 |
Correct |
59 ms |
2560 KB |
Output is correct |
21 |
Correct |
68 ms |
2544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |