//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const int oo = 1e16;
const int mod = 1e9 + 7;
const int K = 1e5 + 5;
char P[N], Q[N];
int S[N], T[N], k, n, ans, s[N];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> k >> n;
vector<int> vr;
vector<pii> h;
int cnt = 0;
for(int i = 1; i <= n; i ++){
cin >> P[i] >> S[i] >> Q[i] >> T[i];
if(P[i] == Q[i]){
ans += abs(S[i] - T[i]);
continue;
}
vr.pb(S[i]);
vr.pb(T[i]);
cnt++;
if(S[i] > T[i]) swap(S[i], T[i]);
h.pb({S[i], T[i]});
}
// for(auto j : vr) cout << j << "\n";
// cout << "\n";
if(!vr.empty()){
sort(vr.begin(), vr.end());
int mx = 0;
for(int i = 1; i <= cnt * 2; i ++) mx = max(mx, vr[i - 1]), s[i] = s[i - 1] + vr[i - 1];
// cout << mx << "k\n";
// for(auto j : vr) cout << j << "\n";
// cout << "\n";
if(k == 1){
int kq = oo;
int lim = vr[cnt - 1];
// cout << lim << " " << s[cnt * 2] << " " << 2 * cnt << "f\n";
// return 0;
for(int j = max(vr[0], lim - (int)1e5); j <= min(vr[2 * cnt - 1], lim + (int)1e5); j ++){
int i = lower_bound(all(vr), j) - vr.begin();
if(i < 2 * cnt){
i++;}
kq = min(kq, cnt + j * (i - 1) - j * (2 * cnt - i + 1) - s[i - 1] + s[cnt * 2] - s[i - 1]);
// }
// else{
// kq = min(kq, cnt + j * i - s[i]);
// }
}
ans = ans + kq;
}else{
int ret = oo;
for(int i = 1; i <= cnt << 1; i ++){
for(int j = i + 1; j <= cnt << 1; j ++){
int kq = 0;
for(auto [l, r] : h){
if(l <= vr[i - 1] && vr[i - 1] <= r) kq += 1 + vr[i - 1] - l + r - vr[i - 1];
else if(l <= vr[j - 1] && vr[j - 1] <= r) kq += 1 + vr[j - 1] - l + r - vr[j - 1];
else if(vr[i - 1] <= l && r <= vr[j - 1]) kq += 1 + min(l - vr[i - 1] + r - vr[i - 1], vr[j - 1] - l + vr[j - 1] - r);
else if(r <= vr[i - 1]) kq += 1 + vr[i - 1] - l + vr[i - 1] - r;
else kq += 1 + l - vr[j - 1] + r - vr[j - 1];
}
ret = min(ret, kq + ans);
}
}
ans = ret;
}}
cout << ans;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
3 ms |
4440 KB |
Output is correct |
5 |
Correct |
4 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4440 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4668 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
11 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
4 ms |
4444 KB |
Output is correct |
5 |
Correct |
3 ms |
4440 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4652 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4568 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
11 |
Correct |
4 ms |
4668 KB |
Output is correct |
12 |
Correct |
18 ms |
8892 KB |
Output is correct |
13 |
Correct |
39 ms |
10428 KB |
Output is correct |
14 |
Correct |
27 ms |
8900 KB |
Output is correct |
15 |
Correct |
24 ms |
8124 KB |
Output is correct |
16 |
Correct |
23 ms |
9920 KB |
Output is correct |
17 |
Correct |
29 ms |
10424 KB |
Output is correct |
18 |
Correct |
41 ms |
9992 KB |
Output is correct |
19 |
Correct |
37 ms |
10344 KB |
Output is correct |
20 |
Correct |
27 ms |
9972 KB |
Output is correct |
21 |
Correct |
36 ms |
10048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4572 KB |
Output is correct |
3 |
Correct |
3 ms |
4572 KB |
Output is correct |
4 |
Correct |
4 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4564 KB |
Output is correct |
7 |
Correct |
3 ms |
4440 KB |
Output is correct |
8 |
Correct |
4 ms |
4696 KB |
Output is correct |
9 |
Correct |
4 ms |
4584 KB |
Output is correct |
10 |
Correct |
5 ms |
4444 KB |
Output is correct |
11 |
Correct |
4 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4440 KB |
Output is correct |
4 |
Correct |
4 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4696 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
4 ms |
4444 KB |
Output is correct |
10 |
Correct |
5 ms |
4444 KB |
Output is correct |
11 |
Correct |
4 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
4444 KB |
Output is correct |
13 |
Execution timed out |
2074 ms |
4444 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
3 ms |
4588 KB |
Output is correct |
4 |
Correct |
4 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
5 ms |
4564 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
4 ms |
4444 KB |
Output is correct |
10 |
Correct |
5 ms |
4560 KB |
Output is correct |
11 |
Correct |
4 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
4444 KB |
Output is correct |
13 |
Execution timed out |
2084 ms |
4444 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |