#include<bits/stdc++.h>
using namespace std;
struct node{
char p,q;
long long s,t;
void inn(){
cin>>p>>s>>q>>t;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int k,n;cin>>k>>n;
vector<node>arr(n);
vector<long long>brr;
for (int i = 0;i<n;++i){
arr[i].inn();
if (arr[i].p!=arr[i].q){
brr.push_back(arr[i].s);
brr.push_back(arr[i].t);
}
}
const long long mxn = 1e18;
long long ans = mxn;
sort(brr.begin(),brr.end());
if (k == 1 || brr.size() <= 1){
for (int i = 0;i<2;++i){
long long curans = 0;
int temp = (int)brr.size()/2 - 1 + i;
for (int j = 0;j<n;++j){
if (arr[j].p == arr[j].q){
curans+=abs(arr[j].t - arr[j].s);
}
else{
curans+=abs(arr[j].t - brr[temp]) + abs(arr[j].s - brr[temp]) + 1;
}
}
ans = min(ans,curans);
}
}
brr.erase(unique(brr.begin(),brr.end()),brr.end());
if (k == 2 && (int)brr.size() > 1 && n<=100){
for (int x = 0;x<(int)brr.size();++x){
for (int y = 0;y<(int)brr.size();++y){
long long curans = 0;
for (int j = 0;j<n;++j){
if (arr[j].p == arr[j].q){
curans+=abs(arr[j].t - arr[j].s);
}
else{
curans+=min(llabs(arr[j].t - brr[x]) + llabs(arr[j].s - brr[x]),llabs(arr[j].t - brr[y]) + llabs(arr[j].s - brr[y])) + 1LL;
}
}
ans = min(curans,ans);
}
}
}
if (k == 2){
vector<pair<int,int>>seg;
for (auto x:arr){
if (x.q!=x.p){
int f = lower_bound(brr.begin(),brr.end(),x.s) - brr.begin();
int t = lower_bound(brr.begin(),brr.end(),x.t) - brr.begin();
seg.push_back({f,t});
}
}
vector<int>pref((int)brr.size() + 1,0);
for (auto x:seg){
pref[x.first]++;
pref[x.second + 1]--;
}
int maxxy1 = -1;
int index1 = -1;
for (int i = 1;i<=(int)brr.size();++i){
pref[i]+=pref[i - 1];
}
for (int i = 0;i<(int)brr.size();++i){
if (pref[i] > maxxy1){
maxxy1 = pref[i];
}
}
vector<int>pos;
for (int i = 0;i<(int)brr.size();++i){
if (pref[i] == maxxy1)pos.push_back(i);
}
for (auto x:pos){
for (int y = 0;y<(int)brr.size();++y){
long long curans = 0;
for (int j = 0;j<n;++j){
if (arr[j].p == arr[j].q){
curans+=abs(arr[j].t - arr[j].s);
}
else{
curans+=min(llabs(arr[j].t - brr[x]) + llabs(arr[j].s - brr[x]),llabs(arr[j].t - brr[y]) + llabs(arr[j].s - brr[y])) + 1LL;
}
}
ans = min(curans,ans);
}
}
}
cout<<ans<<'\n';
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:73:7: warning: unused variable 'index1' [-Wunused-variable]
73 | int index1 = -1;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
20 ms |
4816 KB |
Output is correct |
13 |
Correct |
37 ms |
4816 KB |
Output is correct |
14 |
Correct |
27 ms |
4512 KB |
Output is correct |
15 |
Correct |
23 ms |
2772 KB |
Output is correct |
16 |
Correct |
26 ms |
4864 KB |
Output is correct |
17 |
Correct |
26 ms |
4840 KB |
Output is correct |
18 |
Correct |
41 ms |
4808 KB |
Output is correct |
19 |
Correct |
37 ms |
4816 KB |
Output is correct |
20 |
Correct |
26 ms |
4816 KB |
Output is correct |
21 |
Correct |
44 ms |
4816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
9 ms |
328 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
17 ms |
324 KB |
Output is correct |
9 |
Correct |
12 ms |
212 KB |
Output is correct |
10 |
Correct |
17 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
17 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
8 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
21 ms |
212 KB |
Output is correct |
9 |
Correct |
16 ms |
332 KB |
Output is correct |
10 |
Correct |
17 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
16 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
11 ms |
328 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
17 ms |
212 KB |
Output is correct |
9 |
Correct |
12 ms |
324 KB |
Output is correct |
10 |
Correct |
16 ms |
324 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
16 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |