#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,k;
ll ans=0,cnt=1e9;
vector<ll> sum1,sum2;
vector<int> v,v1;
vector<int> :: iterator itt;
set<int> s;
set<int> :: iterator it;
void go(){
ll hey=0;
for(it=s.begin(); it!=s.end(); it++){
hey=0;
itt=upper_bound(v.begin(),v.end(), *it);
if(itt!=v.end()){
int pos=itt-v.begin();
ll tur=0;
if(pos==0){
tur+=sum1[v.size()-1];
}
else{
tur+=sum1[v.size()-1]-sum1[pos-1];
}
hey+=tur-(v.size()-pos)*(*it);
}
itt=lower_bound(v.begin(), v.end(), *it);
if(itt!=v.begin()){
itt--;
int pos=itt-v.begin();
ll tur=sum1[pos];
hey+=*it*(pos+1)-tur;
}
//
itt=upper_bound(v1.begin(),v1.end(), *it);
if(itt!=v1.end()){
int pos=itt-v1.begin();
ll tur=0;
if(pos==0){
tur+=sum2[v1.size()-1];
}
else{
tur+=sum2[v1.size()-1]-sum2[pos-1];
}
hey+=tur-(v1.size()-pos)*(*it);
}
itt=lower_bound(v1.begin(), v1.end(), *it);
if(itt!=v1.begin()){
itt--;
int pos=itt-v1.begin();
ll tur=sum2[pos];
hey+=*it*(pos+1)-tur;
}
cnt=min(cnt,hey);
}
}
int main(){
IOS
cin>>k>>n;
for(int i=1; i<=n; i++){
char c1,c2;
int x,y;
cin>>c1>>x>>c2>>y;
if(c1==c2){
ans+=abs(x-y);
continue;
}
v.pb(x);
v1.pb(y);
s.insert(x);
s.insert(y);
// x<=y
}
if(k==1){
if(v.size()==0){
cout<<ans;
return 0;
}
ans+=v.size();
sort(v.begin(), v.end());
sort(v1.begin(), v1.end());
sum1.pb(v[0]);
sum2.pb(v1[0]);
for(int i=1; i<v.size(); i++){
sum1.pb(sum1[i-1]+v[i]);
}
for(int i=1; i<v1.size(); i++){
sum2.pb(sum2[i-1]+v1[i]);
}
go();
ans+=cnt;
cout<<ans;
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i=1; i<v.size(); i++){
| ~^~~~~~~~~
bridge.cpp:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=1; i<v1.size(); i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
456 KB |
Output is correct |
5 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Incorrect |
2 ms |
452 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |