#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i --)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#ifdef wiwihorz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("["+string(#a)+"] = ", a)
void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1,class ... T2>void kout(T1 a,T2 ... e){cerr<<a<<" ",kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
namespace solver1 {
int n, k, ans;
vector<pii> v;
vector<int> a;
void init_(int _n, int _k) {
ans = 0;
n = _n, k = _k;
assert(k == 1);
v.clear();
}
void solve() {
n = signed(v.size());
rep(i, 0, n - 1) {
a.push_back(v[i].x);
a.push_back(v[i].y);
}
sort(all(a));
int mid = a[(n * 2 - 1) / 2];
for(auto i : a) ans += abs(mid - i);
cout << ans + n << "\n";
}
};
namespace solver2 {
int n, k, ans, sa, sb;
vector<pii> v;
vector<int> L, R;
multiset<int> a, b;
void init_(int _n, int _k) {
ans = 0, sa = 0, sb = 0;
n = _n, k = _k;
assert(k == 2);
}
void insert(int x) {
if(!a.size()) {
a.insert(x);
sa += x;
}
else {
if(x <= *a.rbegin()) {
a.insert(x);
sa += x;
}
else {
b.insert(x);
sb += x;
}
if(a.size() > b.size() + 1) {
auto it = prev(a.end());
sa -= *it, sb += *it;
b.insert(*it);
a.erase(it);
}
if(b.size() > a.size()) {
auto it = b.begin();
sb -= *it, sa += *it;
a.insert(*it);
b.erase(it);
}
}
}
void solve() {
n = v.size();
L.assign(n, 0);
R.assign(n, 0);
sa = 0, sb = 0;
sort(all(v), [&](pii a, pii b){
return a.x + a.y < b.x + b.y;
});
rep(i, 0, n - 1) {
insert(v[i].x);
insert(v[i].y);
L[i] = sb - sa;
}
sa = 0, sb = 0;
a.clear();
b.clear();
rrep(i, 0, n - 1) {
insert(v[i].x);
insert(v[i].y);
R[i] = sb - sa;
}
int best = INF;
rep(i, 0, n) {
int val = 0;
if(i - 1 >= 0) val += L[i - 1];
if(i < n) val += R[i];
best = min(best, val);
}
cout << ans + best + n << "\n";
}
};
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int k, n;
cin >> k >> n;
if(k == 1) {
using namespace solver1;
init_(n, k);
rep(i, 1, n) {
char x, y;
int a, b;
cin >> x >> a;
cin >> y >> b;
if(x == y) ans += abs(a - b);
else v.push_back({a, b});
}
solve();
}
else {
using namespace solver2;
init_(n, k);
rep(i, 1, n) {
char x, y;
int a, b;
cin >> x >> a;
cin >> y >> b;
if(x == y) ans += abs(a - b);
else v.push_back({a, b});
}
solve();
}
return 0;
}
Compilation message
bridge.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
5 | #pragma loop-opt(on)
|
bridge.cpp:20:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
bridge.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
24 ms |
5692 KB |
Output is correct |
13 |
Correct |
43 ms |
6744 KB |
Output is correct |
14 |
Correct |
32 ms |
5964 KB |
Output is correct |
15 |
Correct |
27 ms |
4052 KB |
Output is correct |
16 |
Correct |
29 ms |
6532 KB |
Output is correct |
17 |
Correct |
30 ms |
6588 KB |
Output is correct |
18 |
Correct |
39 ms |
6644 KB |
Output is correct |
19 |
Correct |
53 ms |
6644 KB |
Output is correct |
20 |
Correct |
32 ms |
6612 KB |
Output is correct |
21 |
Correct |
38 ms |
6564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
356 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
356 KB |
Output is correct |
24 |
Correct |
2 ms |
464 KB |
Output is correct |
25 |
Correct |
122 ms |
13508 KB |
Output is correct |
26 |
Correct |
240 ms |
13804 KB |
Output is correct |
27 |
Correct |
261 ms |
14684 KB |
Output is correct |
28 |
Correct |
274 ms |
15072 KB |
Output is correct |
29 |
Correct |
261 ms |
15040 KB |
Output is correct |
30 |
Correct |
96 ms |
8136 KB |
Output is correct |
31 |
Correct |
109 ms |
14448 KB |
Output is correct |
32 |
Correct |
140 ms |
15060 KB |
Output is correct |
33 |
Correct |
123 ms |
14784 KB |
Output is correct |
34 |
Correct |
132 ms |
15168 KB |
Output is correct |
35 |
Correct |
133 ms |
14732 KB |
Output is correct |
36 |
Correct |
130 ms |
14816 KB |
Output is correct |
37 |
Correct |
131 ms |
13600 KB |
Output is correct |