#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a; i<int(b); i++)
#define all(x) x.begin(),x.end()
vector <pair<long long,long long> > travels;
vector <long long> points;
long long cost(long long point){
long long ans=0;
for (auto a:travels) {
long long c,d;
c=a.first;
d=a.second;
ans+=abs(point-c) + abs(point-d);
}
return ans;
}
long long cost (long long point1, long long point2) {
long long ans=0;
for (auto a:travels) {
long long c,d;
c=a.first;
d=a.second;
ans+=min(abs(point1-c) + abs(point1-d),abs(point2-c) + abs(point2-d));
}
return ans;
}
int main() {
unordered_set<long long> unip;
int k,n;
cin >> k >> n;
long long sum=0;
rep(i,0,n) {
char a,b;
long long c,d;
cin >> a >> c >> b >> d;
if (a==b) {
sum+=abs(c-d);
}
else {
travels.push_back(make_pair(min(c,d),max(c,d)));
sum+=1;
unip.insert(c);
unip.insert(d);
}
}
for (auto x:unip) {
points.push_back(x);
}
if (k==1) {
sort(all(points));
int lo,hi;
lo=0;
hi=points.size()-1;
while (lo+2<hi) {
int mid1=lo+(hi-lo)/3;
int mid2=lo+(2*hi-2*lo)/3;
long long val1,val2;
val1=cost(points[mid1]);
val2=cost(points[mid2]);
if (val1<val2) hi=mid2;
else lo=mid1;
}
long long best=1000000000000000;
rep (i,lo,hi+1) {
best=min(best,cost(points[i]));
}
if (best==1000000000000000) best=0;
cout << best+sum << endl;
}
else {
sort(all(points));
//Hillclimb, two indicies: a and b, a<=b
if (points.size()==0) {
cout << sum << endl;
}
else {
long long best=cost(points[0],points[0]);
int besta,bestb;
besta=0;
bestb=0;
int m=points.size();
int stepsize=2*points.size()/3;
while (stepsize>2) {
int tempa,tempb;
tempa=besta;
tempb=bestb;
rep(i,-1,2) {
rep(j,-1,2) {
int a,b;
a=besta+i*stepsize;
b=bestb+j*stepsize;
if (a>b) continue;
if (a<0 || b>=m) continue;
long long currcost=cost(points[a],points[b]);
if (currcost<best) {
best=currcost;
tempa=a;
tempb=b;
}
}
}
besta=tempa;
bestb=tempb;
stepsize/=2;
stepsize+=1;
}
rep (i, max(0,besta-3),min(m,besta+3)) {
rep (j, max(0,bestb-3), min(m,bestb+3)) {
best=min(best,cost(points[i],points[j]));
}
}
cout << sum+best << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
472 KB |
Output is correct |
3 |
Correct |
4 ms |
472 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
7 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
6 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
768 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
768 KB |
Output is correct |
11 |
Correct |
4 ms |
768 KB |
Output is correct |
12 |
Correct |
89 ms |
2780 KB |
Output is correct |
13 |
Correct |
375 ms |
13256 KB |
Output is correct |
14 |
Correct |
163 ms |
13256 KB |
Output is correct |
15 |
Correct |
259 ms |
13256 KB |
Output is correct |
16 |
Correct |
120 ms |
13256 KB |
Output is correct |
17 |
Correct |
414 ms |
13420 KB |
Output is correct |
18 |
Correct |
264 ms |
13420 KB |
Output is correct |
19 |
Correct |
391 ms |
13420 KB |
Output is correct |
20 |
Correct |
178 ms |
13420 KB |
Output is correct |
21 |
Correct |
257 ms |
13420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13420 KB |
Output is correct |
2 |
Correct |
5 ms |
13420 KB |
Output is correct |
3 |
Correct |
2 ms |
13420 KB |
Output is correct |
4 |
Correct |
5 ms |
13420 KB |
Output is correct |
5 |
Correct |
3 ms |
13420 KB |
Output is correct |
6 |
Correct |
3 ms |
13420 KB |
Output is correct |
7 |
Correct |
2 ms |
13420 KB |
Output is correct |
8 |
Correct |
4 ms |
13420 KB |
Output is correct |
9 |
Correct |
3 ms |
13420 KB |
Output is correct |
10 |
Correct |
3 ms |
13420 KB |
Output is correct |
11 |
Correct |
3 ms |
13420 KB |
Output is correct |
12 |
Correct |
4 ms |
13420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13420 KB |
Output is correct |
2 |
Correct |
3 ms |
13420 KB |
Output is correct |
3 |
Correct |
3 ms |
13420 KB |
Output is correct |
4 |
Correct |
3 ms |
13420 KB |
Output is correct |
5 |
Correct |
3 ms |
13420 KB |
Output is correct |
6 |
Correct |
3 ms |
13420 KB |
Output is correct |
7 |
Correct |
3 ms |
13420 KB |
Output is correct |
8 |
Correct |
3 ms |
13420 KB |
Output is correct |
9 |
Correct |
3 ms |
13420 KB |
Output is correct |
10 |
Correct |
4 ms |
13420 KB |
Output is correct |
11 |
Correct |
3 ms |
13420 KB |
Output is correct |
12 |
Correct |
3 ms |
13420 KB |
Output is correct |
13 |
Correct |
4 ms |
13420 KB |
Output is correct |
14 |
Correct |
7 ms |
13420 KB |
Output is correct |
15 |
Correct |
7 ms |
13420 KB |
Output is correct |
16 |
Correct |
3 ms |
13420 KB |
Output is correct |
17 |
Correct |
3 ms |
13420 KB |
Output is correct |
18 |
Correct |
5 ms |
13420 KB |
Output is correct |
19 |
Correct |
5 ms |
13420 KB |
Output is correct |
20 |
Correct |
6 ms |
13420 KB |
Output is correct |
21 |
Correct |
5 ms |
13420 KB |
Output is correct |
22 |
Correct |
6 ms |
13420 KB |
Output is correct |
23 |
Correct |
5 ms |
13420 KB |
Output is correct |
24 |
Correct |
5 ms |
13420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13420 KB |
Output is correct |
2 |
Correct |
2 ms |
13420 KB |
Output is correct |
3 |
Correct |
4 ms |
13420 KB |
Output is correct |
4 |
Correct |
3 ms |
13420 KB |
Output is correct |
5 |
Correct |
3 ms |
13420 KB |
Output is correct |
6 |
Correct |
4 ms |
13420 KB |
Output is correct |
7 |
Correct |
2 ms |
13420 KB |
Output is correct |
8 |
Correct |
3 ms |
13420 KB |
Output is correct |
9 |
Correct |
3 ms |
13420 KB |
Output is correct |
10 |
Correct |
3 ms |
13420 KB |
Output is correct |
11 |
Correct |
2 ms |
13420 KB |
Output is correct |
12 |
Correct |
3 ms |
13420 KB |
Output is correct |
13 |
Correct |
4 ms |
13420 KB |
Output is correct |
14 |
Correct |
5 ms |
13420 KB |
Output is correct |
15 |
Correct |
5 ms |
13420 KB |
Output is correct |
16 |
Correct |
2 ms |
13420 KB |
Output is correct |
17 |
Correct |
5 ms |
13420 KB |
Output is correct |
18 |
Correct |
5 ms |
13420 KB |
Output is correct |
19 |
Correct |
4 ms |
13420 KB |
Output is correct |
20 |
Correct |
8 ms |
13420 KB |
Output is correct |
21 |
Correct |
6 ms |
13420 KB |
Output is correct |
22 |
Correct |
7 ms |
13420 KB |
Output is correct |
23 |
Correct |
5 ms |
13420 KB |
Output is correct |
24 |
Correct |
4 ms |
13420 KB |
Output is correct |
25 |
Correct |
108 ms |
13420 KB |
Output is correct |
26 |
Correct |
163 ms |
13420 KB |
Output is correct |
27 |
Correct |
391 ms |
13420 KB |
Output is correct |
28 |
Correct |
415 ms |
13420 KB |
Output is correct |
29 |
Correct |
436 ms |
13420 KB |
Output is correct |
30 |
Correct |
278 ms |
13420 KB |
Output is correct |
31 |
Correct |
122 ms |
13420 KB |
Output is correct |
32 |
Correct |
416 ms |
13420 KB |
Output is correct |
33 |
Correct |
275 ms |
13420 KB |
Output is correct |
34 |
Correct |
426 ms |
13420 KB |
Output is correct |
35 |
Correct |
139 ms |
13420 KB |
Output is correct |
36 |
Correct |
351 ms |
13420 KB |
Output is correct |
37 |
Correct |
115 ms |
13420 KB |
Output is correct |