#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define pii array<int,2>
struct cmp {
bool operator()(const pii &x, const pii &y) {
return (x[0] + x[1]) < (y[0] + y[1]);
}
};
signed main() {
int k, n;cin>>k>>n;
vector<pii> dif;
int dis = 0;
for (int i=0;i<n;i++) {
char c1,c2;
int p1,p2;
cin>>c1>>p1>>c2>>p2;
if (c1 == c2) {
dis += abs(p1-p2);
}
else {
dif.push_back({p1,p2});
}
}
if (dif.size() == 0) {
cout<<dis<<"\n";
return 0;
}
if (k == 1 || dif.size() == 1) {
vector<int> al;
for (auto x:dif) {
al.push_back(x[0]);
al.push_back(x[1]);
}
sort(al.begin(),al.end());
int med = al[al.size() / 2];
for (int x:al) {
dis += abs(x - med);
}
dis += dif.size();
cout<<dis<<"\n";
return 0;
}
sort(dif.begin(),dif.end(), cmp());
multiset<int> m1, m2;
int s1 = 0, s2 = 0;
function<void(int,int)> insert=[&](int num1, int num2) {
m1.insert(num1);
m1.insert(num2);
s1 += num1;
s1 += num2;
int num = *m1.rbegin();
s1 -= num;
s2 += num;
m1.erase(m1.find(num));
m2.insert(num);
while (*m2.begin() < *m1.rbegin()) {
int f = *m2.begin(),s=*m1.rbegin();
m2.erase(m2.find(f));
m1.erase(m1.find(s));
m1.insert(f);
m2.insert(s);
s2 -= f;s1 += f;
s2 += s;s1 -= s;
}
};
vector<int> cst1, cst2;
vector<int> al;
if (dif.size() > 4000) {
for (auto x:dif) {
insert(x[0], x[1]);
assert(m1.size() == m2.size());
cst1.push_back(s2 - s1);
}
}
else {
for (auto x:dif) {
al.push_back(x[0]);
al.push_back(x[1]);
sort(al.begin(),al.end());
int med = al[al.size()/2];
int tmp=0;
for (int y:al) {
tmp += abs(y-med);
}
cst1.push_back(tmp);
}
}
reverse(dif.begin(),dif.end());
m1.clear();m2.clear();
s1=s2=0;
al.clear();
if (dif.size() > 4000) {
for (auto x:dif) {
insert(x[0], x[1]);
assert(m1.size() == m2.size());
cst2.push_back(s2 - s1);
}
}
else {
for (auto x:dif) {
al.push_back(x[0]);
al.push_back(x[1]);
sort(al.begin(),al.end());
int med = al[al.size()/2];
int tmp=0;
for (int y:al) {
tmp += abs(y-med);
}
cst2.push_back(tmp);
}
}
reverse(cst2.begin(),cst2.end());
int res = 1e18;
for (int i=1;i<(int)cst1.size();i++) {
res = min(res, cst1[i-1] + cst2[i]);
}
res += (int)dif.size();
cout<<res+dis<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
504 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
484 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
37 ms |
5836 KB |
Output is correct |
13 |
Correct |
78 ms |
7204 KB |
Output is correct |
14 |
Correct |
52 ms |
6592 KB |
Output is correct |
15 |
Correct |
45 ms |
4040 KB |
Output is correct |
16 |
Correct |
58 ms |
6568 KB |
Output is correct |
17 |
Correct |
68 ms |
7180 KB |
Output is correct |
18 |
Correct |
66 ms |
6852 KB |
Output is correct |
19 |
Correct |
80 ms |
7364 KB |
Output is correct |
20 |
Correct |
59 ms |
6856 KB |
Output is correct |
21 |
Correct |
69 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
440 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
16 ms |
600 KB |
Output is correct |
14 |
Correct |
69 ms |
348 KB |
Output is correct |
15 |
Correct |
63 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
472 KB |
Output is correct |
18 |
Correct |
7 ms |
348 KB |
Output is correct |
19 |
Correct |
17 ms |
348 KB |
Output is correct |
20 |
Correct |
66 ms |
508 KB |
Output is correct |
21 |
Correct |
70 ms |
492 KB |
Output is correct |
22 |
Correct |
63 ms |
348 KB |
Output is correct |
23 |
Correct |
19 ms |
476 KB |
Output is correct |
24 |
Correct |
62 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
16 ms |
348 KB |
Output is correct |
14 |
Correct |
65 ms |
492 KB |
Output is correct |
15 |
Correct |
63 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
360 KB |
Output is correct |
17 |
Correct |
16 ms |
428 KB |
Output is correct |
18 |
Correct |
7 ms |
348 KB |
Output is correct |
19 |
Correct |
17 ms |
488 KB |
Output is correct |
20 |
Correct |
62 ms |
348 KB |
Output is correct |
21 |
Correct |
63 ms |
492 KB |
Output is correct |
22 |
Correct |
63 ms |
496 KB |
Output is correct |
23 |
Correct |
20 ms |
348 KB |
Output is correct |
24 |
Correct |
62 ms |
500 KB |
Output is correct |
25 |
Correct |
140 ms |
13760 KB |
Output is correct |
26 |
Correct |
192 ms |
14004 KB |
Output is correct |
27 |
Correct |
213 ms |
14872 KB |
Output is correct |
28 |
Correct |
214 ms |
15500 KB |
Output is correct |
29 |
Correct |
226 ms |
15300 KB |
Output is correct |
30 |
Correct |
100 ms |
8244 KB |
Output is correct |
31 |
Correct |
152 ms |
14780 KB |
Output is correct |
32 |
Correct |
182 ms |
15272 KB |
Output is correct |
33 |
Correct |
136 ms |
15244 KB |
Output is correct |
34 |
Correct |
190 ms |
15520 KB |
Output is correct |
35 |
Correct |
171 ms |
15036 KB |
Output is correct |
36 |
Correct |
187 ms |
15040 KB |
Output is correct |
37 |
Correct |
140 ms |
13972 KB |
Output is correct |