#include<bits/stdc++.h>
#define all(s) s.begin(),s.end()
using namespace std;
typedef long long ll;
ll n,k,ans,x,y,p[200009],a[200009];
char c1,c2;
vector<ll>v;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>k>>n;
for(ll i=0; i<n; i++)
{
cin>>c1>>x>>c2>>y;
if(c1==c2)
ans+=abs(x-y);
else
{
v.push_back(x);
v.push_back(y);
ans++;//for bridge length
}
}
n=v.size();
v.push_back(-1),sort(all(v));
for(ll i=1; i<v.size(); i++)
p[i]=p[i-1]+v[i];
for(ll i=1; i<v.size(); i++)
{
ll psum=abs(p[i]-i*v[i]);
ll ssum=abs(p[n]-p[i]-(n-i)*v[i]);
a[i] = psum + ssum;
if(k==1)
continue;
ll l=i-1,r=n+1,m;
while(r-l>1)
{
m=(l+r)/2;
ll m2=(v[i]+v[m])/2,id,le=i-1,re=m+1;
while(re-le>1)
{
id=(le+re)/2;
if(v[id]<=m2)
le=id;
else
re=id;
}
id=le;
psum=abs(p[i]-i*v[i])+abs(p[id]-p[i]*(id-i)*v[i]);
ssum=abs(p[m]-p[id]-(m-id)*v[m])+abs(p[n]-p[m-1]-(n-m)*v[m]);
if(a[i]<=psum+ssum)
{
a[i]=psum+ssum;
l=m;
}
else
r=m;
}
}
for(ll i=2; i<=n; i++)
a[i]=min(a[i],a[i-1]);
cout<<a[n]+ans;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:28:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(ll i=1; i<v.size(); i++)
| ~^~~~~~~~~
bridge.cpp:30:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(ll i=1; i<v.size(); i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
376 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
372 KB |
Output is correct |
11 |
Correct |
1 ms |
376 KB |
Output is correct |
12 |
Correct |
29 ms |
5820 KB |
Output is correct |
13 |
Correct |
51 ms |
7448 KB |
Output is correct |
14 |
Correct |
38 ms |
5792 KB |
Output is correct |
15 |
Correct |
30 ms |
4552 KB |
Output is correct |
16 |
Correct |
37 ms |
6752 KB |
Output is correct |
17 |
Correct |
51 ms |
7380 KB |
Output is correct |
18 |
Correct |
45 ms |
7016 KB |
Output is correct |
19 |
Correct |
50 ms |
7500 KB |
Output is correct |
20 |
Correct |
37 ms |
6988 KB |
Output is correct |
21 |
Correct |
46 ms |
7096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |