#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int k, n;
int sz;
long long ans;
pair<int,int> a[N];
vector<int> z;
vector<int> Q[N];
long long solve1() {
if (n == 0) return 0;
long long res = 0;
int med = z[z.size() / 2];
for (int i = 0; i < z.size(); ++i) {
res += abs(med - z[i]);
}
return res;
}
long long fl[N];
long long fr[N];
int T[N];
long long sum[N];
void upd(int x) { for (; x < N; x += x & -x) T[x]++; }
int get(int x) { int res = 0; for (; x > 0; x -= x & -x) res += T[x]; return res; }
void upd_sum(int x, int v) { for (; x < N; x += x & -x) sum[x] += v; }
long long get_sum(int x) { long long res = 0; for (; x > 0; x -= x & -x) res += sum[x]; return res; }
void prepare(int id) {
long long cur = 0;
memset(T, 0, sizeof T);
memset(sum, 0, sizeof sum);
for (int i = 1; i <= n; ++i) {
int s = lower_bound(z.begin(), z.end(), a[i].first) - z.begin() + 1;
int t = lower_bound(z.begin(), z.end(), a[i].second) - z.begin() + 1;
upd(s); upd(t);
upd_sum(s, a[i].first); upd_sum(t, a[i].second);
cur += a[i].first; cur += a[i].second;
int l = 1, r = z.size();
while(l < r) {
int mid = ((l + r) >> 1);
if (get(mid) >= i) r = mid; else l = mid + 1;
}
int smaller = get(l);
long long cur_sum = get_sum(l);
long long res = ((cur - cur_sum) - 1LL * (2 * i - smaller) * z[l-1]) + (1LL * smaller * z[l-1] - cur_sum);
if (!id) fl[i] = res; else fr[n + 1 - i] = res;
}
}
long long solve2() {
if (n == 0) return 0;
sort(a + 1, a + n + 1, [&](pair<int,int> x, pair<int,int> y) {
return x.first + x.second < y.first + y.second;
});
prepare(0); reverse(a + 1, a + n + 1); prepare(1);
long long res = 1e18;
for (int i = 1; i <= n; ++i) {
res = min(res, fl[i] + fr[i + 1]);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) ans += abs(s - t);
else ++sz, a[sz] = make_pair(min(s, t), max(s, t));
}
n = sz;
for (int i = 1; i <= n; ++i) {
z.push_back(a[i].first);
z.push_back(a[i].second);
}
sort(z.begin(), z.end());
ans += n;
if (k == 1) ans += solve1(); else ans += min(solve1(), solve2());
printf("%lld\n", ans);
}
Compilation message
bridge.cpp: In function 'long long int solve1()':
bridge.cpp:17:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < z.size(); ++i) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13896 KB |
Output is correct |
2 |
Correct |
0 ms |
13896 KB |
Output is correct |
3 |
Correct |
0 ms |
13896 KB |
Output is correct |
4 |
Correct |
0 ms |
13896 KB |
Output is correct |
5 |
Correct |
0 ms |
13896 KB |
Output is correct |
6 |
Correct |
0 ms |
13896 KB |
Output is correct |
7 |
Correct |
0 ms |
13896 KB |
Output is correct |
8 |
Correct |
3 ms |
13896 KB |
Output is correct |
9 |
Correct |
3 ms |
13896 KB |
Output is correct |
10 |
Correct |
0 ms |
13896 KB |
Output is correct |
11 |
Correct |
3 ms |
13896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13896 KB |
Output is correct |
2 |
Correct |
3 ms |
13896 KB |
Output is correct |
3 |
Correct |
0 ms |
13896 KB |
Output is correct |
4 |
Correct |
3 ms |
13896 KB |
Output is correct |
5 |
Correct |
0 ms |
13896 KB |
Output is correct |
6 |
Correct |
0 ms |
13896 KB |
Output is correct |
7 |
Correct |
0 ms |
13896 KB |
Output is correct |
8 |
Correct |
3 ms |
13896 KB |
Output is correct |
9 |
Correct |
3 ms |
13896 KB |
Output is correct |
10 |
Correct |
0 ms |
13896 KB |
Output is correct |
11 |
Correct |
3 ms |
13896 KB |
Output is correct |
12 |
Correct |
23 ms |
15472 KB |
Output is correct |
13 |
Correct |
46 ms |
15472 KB |
Output is correct |
14 |
Correct |
36 ms |
15472 KB |
Output is correct |
15 |
Correct |
23 ms |
14704 KB |
Output is correct |
16 |
Correct |
33 ms |
15472 KB |
Output is correct |
17 |
Correct |
36 ms |
15472 KB |
Output is correct |
18 |
Correct |
39 ms |
15472 KB |
Output is correct |
19 |
Correct |
39 ms |
15472 KB |
Output is correct |
20 |
Correct |
36 ms |
15472 KB |
Output is correct |
21 |
Correct |
43 ms |
15472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13896 KB |
Output is correct |
2 |
Correct |
3 ms |
13896 KB |
Output is correct |
3 |
Correct |
0 ms |
13896 KB |
Output is correct |
4 |
Correct |
0 ms |
13896 KB |
Output is correct |
5 |
Correct |
0 ms |
13896 KB |
Output is correct |
6 |
Correct |
0 ms |
13896 KB |
Output is correct |
7 |
Correct |
0 ms |
13896 KB |
Output is correct |
8 |
Correct |
0 ms |
13896 KB |
Output is correct |
9 |
Correct |
0 ms |
13896 KB |
Output is correct |
10 |
Correct |
3 ms |
13896 KB |
Output is correct |
11 |
Correct |
0 ms |
13896 KB |
Output is correct |
12 |
Correct |
0 ms |
13896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13896 KB |
Output is correct |
2 |
Correct |
0 ms |
13896 KB |
Output is correct |
3 |
Correct |
0 ms |
13896 KB |
Output is correct |
4 |
Correct |
3 ms |
13896 KB |
Output is correct |
5 |
Correct |
0 ms |
13896 KB |
Output is correct |
6 |
Correct |
0 ms |
13896 KB |
Output is correct |
7 |
Correct |
0 ms |
13896 KB |
Output is correct |
8 |
Correct |
3 ms |
13896 KB |
Output is correct |
9 |
Correct |
3 ms |
13896 KB |
Output is correct |
10 |
Correct |
0 ms |
13896 KB |
Output is correct |
11 |
Correct |
0 ms |
13896 KB |
Output is correct |
12 |
Correct |
0 ms |
13896 KB |
Output is correct |
13 |
Correct |
0 ms |
13896 KB |
Output is correct |
14 |
Correct |
0 ms |
13896 KB |
Output is correct |
15 |
Correct |
3 ms |
13896 KB |
Output is correct |
16 |
Correct |
0 ms |
13896 KB |
Output is correct |
17 |
Correct |
0 ms |
13896 KB |
Output is correct |
18 |
Correct |
0 ms |
13896 KB |
Output is correct |
19 |
Correct |
0 ms |
13896 KB |
Output is correct |
20 |
Correct |
3 ms |
13896 KB |
Output is correct |
21 |
Correct |
3 ms |
13896 KB |
Output is correct |
22 |
Correct |
6 ms |
13896 KB |
Output is correct |
23 |
Correct |
3 ms |
13896 KB |
Output is correct |
24 |
Correct |
0 ms |
13896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13896 KB |
Output is correct |
2 |
Correct |
0 ms |
13896 KB |
Output is correct |
3 |
Correct |
0 ms |
13896 KB |
Output is correct |
4 |
Correct |
0 ms |
13896 KB |
Output is correct |
5 |
Correct |
0 ms |
13896 KB |
Output is correct |
6 |
Correct |
0 ms |
13896 KB |
Output is correct |
7 |
Correct |
0 ms |
13896 KB |
Output is correct |
8 |
Correct |
3 ms |
13896 KB |
Output is correct |
9 |
Correct |
0 ms |
13896 KB |
Output is correct |
10 |
Correct |
0 ms |
13896 KB |
Output is correct |
11 |
Correct |
0 ms |
13896 KB |
Output is correct |
12 |
Correct |
3 ms |
13896 KB |
Output is correct |
13 |
Correct |
0 ms |
13896 KB |
Output is correct |
14 |
Correct |
0 ms |
13896 KB |
Output is correct |
15 |
Correct |
0 ms |
13896 KB |
Output is correct |
16 |
Correct |
0 ms |
13896 KB |
Output is correct |
17 |
Correct |
6 ms |
13896 KB |
Output is correct |
18 |
Correct |
6 ms |
13896 KB |
Output is correct |
19 |
Correct |
6 ms |
13896 KB |
Output is correct |
20 |
Correct |
6 ms |
13896 KB |
Output is correct |
21 |
Correct |
3 ms |
13896 KB |
Output is correct |
22 |
Correct |
6 ms |
13896 KB |
Output is correct |
23 |
Correct |
3 ms |
13896 KB |
Output is correct |
24 |
Correct |
3 ms |
13896 KB |
Output is correct |
25 |
Correct |
89 ms |
15472 KB |
Output is correct |
26 |
Correct |
133 ms |
15472 KB |
Output is correct |
27 |
Correct |
209 ms |
15472 KB |
Output is correct |
28 |
Correct |
219 ms |
15472 KB |
Output is correct |
29 |
Correct |
206 ms |
15472 KB |
Output is correct |
30 |
Correct |
119 ms |
14704 KB |
Output is correct |
31 |
Correct |
93 ms |
15472 KB |
Output is correct |
32 |
Correct |
126 ms |
15472 KB |
Output is correct |
33 |
Correct |
109 ms |
15472 KB |
Output is correct |
34 |
Correct |
149 ms |
15472 KB |
Output is correct |
35 |
Correct |
116 ms |
15472 KB |
Output is correct |
36 |
Correct |
143 ms |
15472 KB |
Output is correct |
37 |
Correct |
96 ms |
15472 KB |
Output is correct |