#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
struct hatvac {
multiset<int> stl, str;
ll sml, smr;
hatvac() {
sml = smr = 0;
}
void balance() {
if (stl.size() < str.size()) {
int val = *str.begin();
stl.insert(val);
sml += val;
str.erase(str.begin());
smr -= val;
}
if (stl.size() > str.size()+1) {
int val = *prev(stl.end());
str.insert(val);
smr += val;
stl.erase(prev(stl.end()));
sml -= val;
}
}
void add(int x) {
if (stl.empty()) {
stl.insert(x);
sml += x;
return;
}
if (x <= *prev(stl.end())) stl.insert(x), sml += x;
else str.insert(x), smr += x;
balance();
}
void rem(int x) {
if (x <= *prev(stl.end())) stl.erase(stl.find(x)), sml -= x;
else str.erase(str.find(x)), smr -= x;
balance();
}
ll cost() {
if (stl.empty()) return 0;
ll med = *prev(stl.end());
return (stl.size()*med - sml) + (smr - str.size()*med);
}
};
void solve() {
int k, n;
cin >> k >> n;
VPI vec;
ll oneside = 0;
rep(i, n) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) oneside += abs(x-y);
else vec.pb({x, y}), oneside++;
}
sort(all(vec), [](PII a, PII b){
return a.ff+a.ss < b.ff+b.ss;
});
hatvac a, b;
rep(i, vec.size()) b.add(vec[i].ff), b.add(vec[i].ss);
ll ans = a.cost() + b.cost();
if (k == 1) {
cout << oneside + ans << endl;
return;
}
/* for(auto p : vec) cout << p.first << " " << p.second << " "; cout << endl; */
rep(i, vec.size()) {
/* cout << endl; */
/* for (int x : a.stl) cout << x << " "; cout << "| "; */
/* for (int x : a.str) cout << x << " "; cout << endl; */
/* for (int x : b.stl) cout << x << " "; cout << "| "; */
/* for (int x : b.str) cout << x << " "; cout << endl; */
b.rem(vec[i].ff), b.rem(vec[i].ss);
a.add(vec[i].ff), a.add(vec[i].ss);
setmin(ans, a.cost() + b.cost());
}
cout << oneside + ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll t = 1;
/* cin >> t; */
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
488 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 |
344 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 |
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 |
496 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
55 ms |
11396 KB |
Output is correct |
13 |
Correct |
90 ms |
12832 KB |
Output is correct |
14 |
Correct |
71 ms |
10704 KB |
Output is correct |
15 |
Correct |
48 ms |
7632 KB |
Output is correct |
16 |
Correct |
51 ms |
12236 KB |
Output is correct |
17 |
Correct |
55 ms |
12752 KB |
Output is correct |
18 |
Correct |
57 ms |
12628 KB |
Output is correct |
19 |
Correct |
65 ms |
12732 KB |
Output is correct |
20 |
Correct |
61 ms |
12492 KB |
Output is correct |
21 |
Correct |
59 ms |
12500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
544 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
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 |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
356 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
476 KB |
Output is correct |
25 |
Correct |
117 ms |
11228 KB |
Output is correct |
26 |
Correct |
183 ms |
11592 KB |
Output is correct |
27 |
Correct |
214 ms |
12596 KB |
Output is correct |
28 |
Correct |
211 ms |
12744 KB |
Output is correct |
29 |
Correct |
205 ms |
12752 KB |
Output is correct |
30 |
Correct |
95 ms |
6860 KB |
Output is correct |
31 |
Correct |
109 ms |
12216 KB |
Output is correct |
32 |
Correct |
122 ms |
12860 KB |
Output is correct |
33 |
Correct |
97 ms |
12484 KB |
Output is correct |
34 |
Correct |
139 ms |
13044 KB |
Output is correct |
35 |
Correct |
133 ms |
12496 KB |
Output is correct |
36 |
Correct |
131 ms |
12488 KB |
Output is correct |
37 |
Correct |
128 ms |
11216 KB |
Output is correct |