#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int inff = 1e18;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k, n;
cin >> k >> n;
int ans = 0;
vector<int> a, b;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) {
ans += abs(s - t);
} else {
if (s > t) swap(s, t);
a.push_back(s);
b.push_back(t);
}
}
if (k == 1) {
int m = (int)a.size();
vector<int> c;
for (int i = 0; i < m; i++) {
c.push_back(a[i]);
c.push_back(b[i]);
}
sort(c.begin(), c.end());
ans += m;
for (int i = 0; i < 2 * m; i++) {
ans += abs(c[i] - c[m]);
}
cout << ans << '\n';
} else if (k == 2) {
int m = (int)a.size();
vector<pair<int, int>> c;
for (int i = 0; i < m; i++) {
c.push_back({a[i], b[i]});
}
sort(c.begin(), c.end(), [&](pair<int, int> x, pair<int, int> y) {
return x.fi + x.se < y.fi + y.se;
});
int sl = 0, sr = 0;
vector<int> pref(m);
priority_queue<int> le;
priority_queue<int, vector<int>, greater<int>> ri;
for (int i = 0; i < m; i++) {
auto [a, b] = c[i];
sl += a;
le.push(a);
sr += b;
ri.push(b);
while (le.top() > ri.top()) {
sr += le.top();
ri.push(le.top());
sl += ri.top();
le.push(ri.top());
sr -= ri.top();
ri.pop();
sl -= le.top();
le.pop();
}
pref[i] = ((i + 1) * le.top() - sl) + (sr - (i + 1) * le.top());
}
sl = sr = 0;
while (!le.empty()) le.pop();
while (!ri.empty()) ri.pop();
vector<int> suf(m);
for (int i = m - 1; i >= 0; i--) {
auto [a, b] = c[i];
sl += a;
le.push(a);
sr += b;
ri.push(b);
while (le.top() > ri.top()) {
sr += le.top();
ri.push(le.top());
sl += ri.top();
le.push(ri.top());
sr -= ri.top();
ri.pop();
sl -= le.top();
le.pop();
}
suf[i] = ((m - i) * le.top() - sl) + (sr - (m - i) * le.top());
}
int mn = inff;
ans += m;
for (int i = 0; i < m - 1; i++) mn = min(mn, pref[i] + suf[i + 1]);
if (m == 0) mn = 0;
ans += mn;
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
4220 KB |
Output is correct |
13 |
Correct |
31 ms |
4168 KB |
Output is correct |
14 |
Correct |
22 ms |
3912 KB |
Output is correct |
15 |
Correct |
17 ms |
2392 KB |
Output is correct |
16 |
Correct |
17 ms |
4172 KB |
Output is correct |
17 |
Correct |
21 ms |
4164 KB |
Output is correct |
18 |
Correct |
23 ms |
4168 KB |
Output is correct |
19 |
Correct |
28 ms |
4188 KB |
Output is correct |
20 |
Correct |
19 ms |
4164 KB |
Output is correct |
21 |
Correct |
26 ms |
4168 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 |
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 |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
392 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
356 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
356 KB |
Output is correct |
25 |
Correct |
27 ms |
7224 KB |
Output is correct |
26 |
Correct |
51 ms |
7232 KB |
Output is correct |
27 |
Correct |
62 ms |
7084 KB |
Output is correct |
28 |
Correct |
64 ms |
7228 KB |
Output is correct |
29 |
Correct |
67 ms |
7480 KB |
Output is correct |
30 |
Correct |
32 ms |
4064 KB |
Output is correct |
31 |
Correct |
21 ms |
7240 KB |
Output is correct |
32 |
Correct |
58 ms |
7232 KB |
Output is correct |
33 |
Correct |
33 ms |
7328 KB |
Output is correct |
34 |
Correct |
63 ms |
7232 KB |
Output is correct |
35 |
Correct |
42 ms |
7240 KB |
Output is correct |
36 |
Correct |
62 ms |
7244 KB |
Output is correct |
37 |
Correct |
17 ms |
7232 KB |
Output is correct |