#include<bits/stdc++.h>
#define MAXN 100007
using namespace std;
const long long inf=1e15;
struct person{
char s;
int from;
char t;
int to;
}a[MAXN];
struct event{
int pos;
int id,type;
inline friend bool operator < (event fr,event sc){
return fr.pos<sc.pos;
}
};
int n,k,br[MAXN];
char type;
long long sum,last,lt,rt,cntl,cntr,ans;
vector<event> v;
void solve1(){
ans=inf;
if(v.empty()){
cout<<sum<<"\n";
return;
}
last=v[int(v.size())-1].pos;
for(int i=int(v.size())-1;i>=0;i--){
rt+=(last-v[i].pos)*cntr;
br[v[i].id]++;
if(br[v[i].id]==2){
cntr+=2;
rt+=a[v[i].id].from+a[v[i].id].to-2*v[i].pos;
rt++;
}
last=v[i].pos;
}
last=v[0].pos;
for(int i=0;i<v.size();i++){
lt+=(v[i].pos-last)*cntl;
rt-=(v[i].pos-last)*cntr;
br[v[i].id]--;
if(br[v[i].id]==1){
rt-=abs(a[v[i].id].from-a[v[i].id].to); cntr-=2; rt--;
sum+=abs(a[v[i].id].from-a[v[i].id].to)+1;
}else{
sum-=abs(a[v[i].id].from-a[v[i].id].to)+1;
lt+=abs(a[v[i].id].from-a[v[i].id].to); cntl+=2;
lt++;
}
ans=min(ans,lt+rt+sum);
last=v[i].pos;
}
cout<<ans<<"\n";
}
void solve2(){
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>k>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].from>>a[i].t>>a[i].to;
if(a[i].s==a[i].t){
sum+=abs(a[i].from-a[i].to);
}else{
v.push_back({a[i].from,i,1});
v.push_back({a[i].to,i,2});
}
}
sort(v.begin(),v.end());
if(k==1){
solve1();
}else{
solve2();
}
return 0;
}
Compilation message
bridge.cpp: In function 'void solve1()':
bridge.cpp:49:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 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 |
392 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
26 ms |
5472 KB |
Output is correct |
13 |
Correct |
44 ms |
7032 KB |
Output is correct |
14 |
Correct |
41 ms |
5604 KB |
Output is correct |
15 |
Correct |
26 ms |
4256 KB |
Output is correct |
16 |
Correct |
32 ms |
6308 KB |
Output is correct |
17 |
Correct |
32 ms |
6976 KB |
Output is correct |
18 |
Correct |
39 ms |
6712 KB |
Output is correct |
19 |
Correct |
41 ms |
7068 KB |
Output is correct |
20 |
Correct |
33 ms |
6592 KB |
Output is correct |
21 |
Correct |
38 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |