#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 21, MOD = (int)1e9+7;
#define int ll
int n,k;
ll res = 0;
array<int,2> a[N];
ll L[N],R[N];
vector<pair<int,int>> sc;
void prec(){
for(int i = 1;i <= n;i++){
sc.push_back({a[i][0],0});
sc.push_back({a[i][1],1});
}
sort(sc.begin(),sc.end());
ll sum = 0,col = 0;
for(int i = 0;i < (int)sc.size();i++){
L[i] = (sc[i].first * 1ll * col) - sum;
if(sc[i].second == 1){
col++;
sum += sc[i].first;
}
}
col = sum =0 ;
for(int i = (int)sc.size() - 1;i >= 0;i--){
R[i] = sum - (sc[i].first * 1ll * col);
if(sc[i].second == 0){
col++;
sum += sc[i].first;
}
}
}
ll solve(){
if(!n) return 0;
ll res = 1e18;
for(int i = 0;i < (int)sc.size();i++){
res = min(res,2*(L[i] + R[i]));
}
return res;
}
ll calc(int l,int r){
ll sum = 0;
for(int i = 1;i <= n;i++){
if(a[i][0] > l && a[i][1] < r){
sum += min(2ll*(a[i][0] - l),2*1ll*(r - a[i][1]));
}
}
return sum;
}
int pp[N];
void test() {
cin >> k >> n;
int _i = 0;
for(int i = 1;i <= n;i++) {
char x,y;
int l,r;
cin >> x >> l >> y >> r;
if(l > r){
swap(l,r);
swap(x,y);
}
res += (r - l);
if(x == y){
continue;
}
res++;
a[++_i] = {l,r};
}
n = _i;
prec();
if(k == 1){
cout << res + solve();
return;
}
if(n == 0){
cout << res;
return;
}
ll cur = 1e18,d = 1e18;
int j = -1;
for(int i = 0;i < (int)sc.size();i++){
cur = 1e18;
j = i - 1;
int pos;
while(j + 1 < (int)sc.size()){
ll val = L[i] * 2ll + R[j + 1] * 2ll + calc(sc[i].first,sc[j + 1].first);
if(val <= cur){
pp[i] = j + 1;
}
cur = min(cur,val);
j++;
}
d = min(d,cur);
if(i){
assert(pp[i] >= pp[i - 1]);
}
}
cout << res + d << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
Compilation message
bridge.cpp: In function 'void test()':
bridge.cpp:88:13: warning: unused variable 'pos' [-Wunused-variable]
88 | int pos;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
20 ms |
9184 KB |
Output is correct |
13 |
Correct |
35 ms |
10440 KB |
Output is correct |
14 |
Correct |
29 ms |
8644 KB |
Output is correct |
15 |
Correct |
22 ms |
6356 KB |
Output is correct |
16 |
Correct |
21 ms |
9928 KB |
Output is correct |
17 |
Correct |
24 ms |
10444 KB |
Output is correct |
18 |
Correct |
30 ms |
10188 KB |
Output is correct |
19 |
Correct |
43 ms |
10624 KB |
Output is correct |
20 |
Correct |
24 ms |
10192 KB |
Output is correct |
21 |
Correct |
30 ms |
10276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
496 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
348 KB |
Output is correct |
13 |
Correct |
1662 ms |
580 KB |
Output is correct |
14 |
Correct |
1945 ms |
856 KB |
Output is correct |
15 |
Correct |
1868 ms |
612 KB |
Output is correct |
16 |
Correct |
89 ms |
348 KB |
Output is correct |
17 |
Correct |
286 ms |
548 KB |
Output is correct |
18 |
Correct |
131 ms |
344 KB |
Output is correct |
19 |
Correct |
1372 ms |
604 KB |
Output is correct |
20 |
Correct |
1683 ms |
604 KB |
Output is correct |
21 |
Correct |
1588 ms |
604 KB |
Output is correct |
22 |
Execution timed out |
2059 ms |
604 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
420 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
500 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1625 ms |
604 KB |
Output is correct |
14 |
Correct |
1931 ms |
600 KB |
Output is correct |
15 |
Correct |
1814 ms |
604 KB |
Output is correct |
16 |
Correct |
45 ms |
348 KB |
Output is correct |
17 |
Correct |
267 ms |
544 KB |
Output is correct |
18 |
Correct |
93 ms |
512 KB |
Output is correct |
19 |
Correct |
1322 ms |
604 KB |
Output is correct |
20 |
Correct |
1611 ms |
852 KB |
Output is correct |
21 |
Correct |
1510 ms |
604 KB |
Output is correct |
22 |
Execution timed out |
2019 ms |
600 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |