#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double lld;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define rep(i, from, to) for(int i = from; i < to; i++)
#define inf 1000000000000000000
#define sz(x) (ll)(x).size()
int main(){
char a,b; ll n,c,d,k,s = 0,s1 = 0;
cin >> k >> n;
vi Q;
rep(i, 0, n){
cin >> a >> c >> b >> d;
if (a == b) s += abs(d-c);
else { Q.push_back(c); Q.push_back(d); }
}
sort(Q.begin(), Q.end());
s += sz(Q)/2;
if (k == 1) rep(i, 0, sz(Q)) s += abs(Q[i]-Q[sz(Q)/2]);
else {
vi d1, d2;
d1.assign(sz(Q), 0);
d2.assign(sz(Q), 0);
d1[0] = 0; d2.back() = 0;
rep(i, 1, sz(Q)){
d1[i] = d1[i-1]+Q[i]-Q[0];
d2[sz(Q)-i-1] = d2[sz(Q)-i]+Q.back()-Q[sz(Q)-i-1];
}
s1 = inf;
rep(i, 0, sz(Q)-1){
ll temp = 0; ll b1 = i/2, b2 = (sz(Q)+i)/2;
temp += d1[i]-d1[b1];
temp -= (Q[b1]-Q[0])*(i-b1);
temp += d1.back()-d1[b2];
temp -= (Q[b2]-Q[0])*(sz(Q)-b2-1);
temp += d2[0]-d2[b1];
temp -= (Q.back()-Q[b1])*b1;
temp += d2[i+1]-d2[b2];
temp -= (Q.back()-Q[b2])*(b2-i-1);
s1 = min(temp, s1);
}
}
cout << s+s1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
4 ms |
432 KB |
Output is correct |
5 |
Correct |
5 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
556 KB |
Output is correct |
7 |
Correct |
4 ms |
556 KB |
Output is correct |
8 |
Correct |
3 ms |
556 KB |
Output is correct |
9 |
Correct |
3 ms |
564 KB |
Output is correct |
10 |
Correct |
4 ms |
564 KB |
Output is correct |
11 |
Correct |
4 ms |
564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
564 KB |
Output is correct |
2 |
Correct |
2 ms |
564 KB |
Output is correct |
3 |
Correct |
4 ms |
564 KB |
Output is correct |
4 |
Correct |
4 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
592 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
5 ms |
592 KB |
Output is correct |
8 |
Correct |
5 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
592 KB |
Output is correct |
10 |
Correct |
5 ms |
592 KB |
Output is correct |
11 |
Correct |
5 ms |
592 KB |
Output is correct |
12 |
Correct |
88 ms |
2656 KB |
Output is correct |
13 |
Correct |
172 ms |
2772 KB |
Output is correct |
14 |
Correct |
120 ms |
2772 KB |
Output is correct |
15 |
Correct |
105 ms |
2772 KB |
Output is correct |
16 |
Correct |
123 ms |
2772 KB |
Output is correct |
17 |
Correct |
190 ms |
2780 KB |
Output is correct |
18 |
Correct |
157 ms |
2784 KB |
Output is correct |
19 |
Correct |
191 ms |
2784 KB |
Output is correct |
20 |
Correct |
140 ms |
2784 KB |
Output is correct |
21 |
Correct |
159 ms |
2836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
2836 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
2836 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
2836 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |