#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int inf = 1e9 * 2;
const int mod = 1e9 + 7;
const int N = 3e4 + 5;
ll dis(pair<ll, ll> i, ll p){
if(i.first <= p && i.second >= p) return 0;
else if(i.first > p) return i.first - p;
else return p - i.second;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie();
int k, n;
cin>>k>>n;
ll ans = 0;
vector<pair<int, int> > a;
char h, o;
int hx, ox;
for(int i = 0; i < n; i++){
cin>>h>>hx>>o>>ox;
if(h != o){
a.pb(mp(hx, ox));
}
ans += abs(hx - ox);
}
ans += a.size();
if(!a.size()){
cout<<ans<<endl;
}
else if(k == 1){
vector<int> points;
for(auto itr: a){
points.pb(itr.first);
points.pb(itr.second);
}
sort(points.begin(), points.end());
int med = points[points.size()/2];
ll add = 0;
for(auto itr: a){
add += dis(itr, med) * 2;
}
med = points[points.size()/2-1];
ll add2 = 0;
for(auto itr: a){
add2 += dis(itr, med) * 2;
}
cout<<ans + min(add, add2)<<endl;
}
else{
cout<<0<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |