#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t z;
const int &mxn = 2e5+5;
int n, k;
vector<pair<ll, ll> > v;
ll lef[mxn];
ll rig[mxn];
void solve(){
cin >> k >> n;
ll sum = 0;
fr(i, 0, n){
char c1, c2;
ll a, b;
cin >> c1 >> a >> c2 >> b;
if(c1 == c2){
sum += abs(a-b);
continue;
}
if(a > b) swap(a, b);
v.pb({a, b});
sum += b-a+1;
}
if(k == 1){
if(v.empty()){
cout<<sum<<endl;
return;
}
vector<pair<ll,int> > g;
for(auto u : v){
g.pb({u.st, 0});
g.pb({u.nd, 1});
}
sort(all(g));
int m = g.size();
ll temp_sum = 0;
ll cnt = 0;
fr(i, 0, m){
ll pos = g[i].st;
lef[i] = pos*cnt - temp_sum;
if(g[i].nd == 1){
temp_sum += pos;
++cnt;
}
}
temp_sum = 0;
cnt = 0;
for(int i = m-1; i >= 0; i --){
ll pos = g[i].st;
rig[i] = temp_sum - pos*cnt;
if(g[i].nd == 0){
temp_sum += pos;
++cnt;
}
}
ll ans = inf;
fr(i, 0, m){
ans = min(ans, 2*(lef[i]+rig[i]) + sum);
}
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
388 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
32 ms |
9156 KB |
Output is correct |
13 |
Correct |
55 ms |
10544 KB |
Output is correct |
14 |
Correct |
44 ms |
8628 KB |
Output is correct |
15 |
Correct |
32 ms |
6396 KB |
Output is correct |
16 |
Correct |
39 ms |
9836 KB |
Output is correct |
17 |
Correct |
40 ms |
10564 KB |
Output is correct |
18 |
Correct |
47 ms |
10220 KB |
Output is correct |
19 |
Correct |
61 ms |
10588 KB |
Output is correct |
20 |
Correct |
41 ms |
10152 KB |
Output is correct |
21 |
Correct |
50 ms |
10368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |