#include <bits/stdc++.h>
using namespace std;
#define int long long
int const N=2e5+5;
int const inf=1e16;
vector<int> arr;
int pre[N];
void solve(){
int n;
cin>>n;
vector<pair<int,int>> pr;
set<int> st;
int ex=0;
for (int i = 0; i < n; ++i)
{
char c,d;
int a,b;
cin>>c>>a>>d>>b;
if(c==d)
ex+=abs(a-b);
else{
pr.push_back({a,b});
st.insert(a);
st.insert(b);
}
}
n=pr.size();
int ans=inf;
for(int b1:st)
for(int b2:st){
int temp=n;
for(auto [x,y]:pr)
temp+=min(abs(x-b1)+abs(y-b1),abs(x-b2)+abs(y-b2));
ans=min(ans,temp);
}
if(ans==inf)
ans=0;
cout<<ans+ex<<endl;
}
signed main(){
int k,n;
cin>>k;
if(k==2){
solve();
return 0;
}
cin>>n;
arr.push_back(0);
char c,d;
int a,b;
int ex=n;
for (int i = 1; i <=n; ++i){
cin>>c>>a>>d>>b;
if(c==d){
ex--;
ex+=abs(a-b);
}
else{
arr.push_back(a);
arr.push_back(b);
}
}
sort(arr.begin(), arr.end());
n=(arr.size())-1;
for (int i = 1; i <=n; ++i)
pre[i]=pre[i-1]+arr[i];
int ans=pre[n];
// cout<<ans<<endl;
for(int i=1;i<=n;i++){
ans=min(ans,((arr[i]*i)-pre[i])+((pre[n]-pre[i])-(arr[i]*(n-i))));
// cout<<pre[i]<<endl;
}
cout<<ans+ex<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
29 ms |
3556 KB |
Output is correct |
13 |
Correct |
65 ms |
3608 KB |
Output is correct |
14 |
Correct |
41 ms |
3276 KB |
Output is correct |
15 |
Correct |
39 ms |
2160 KB |
Output is correct |
16 |
Correct |
56 ms |
3516 KB |
Output is correct |
17 |
Correct |
56 ms |
3520 KB |
Output is correct |
18 |
Correct |
56 ms |
3532 KB |
Output is correct |
19 |
Correct |
66 ms |
3524 KB |
Output is correct |
20 |
Correct |
49 ms |
3516 KB |
Output is correct |
21 |
Correct |
61 ms |
3520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
448 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
9 ms |
448 KB |
Output is correct |
9 |
Correct |
9 ms |
416 KB |
Output is correct |
10 |
Correct |
8 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
9 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
412 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
9 ms |
348 KB |
Output is correct |
9 |
Correct |
9 ms |
460 KB |
Output is correct |
10 |
Correct |
8 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
10 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
23 ms |
488 KB |
Output is correct |
15 |
Execution timed out |
2055 ms |
348 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
9 ms |
456 KB |
Output is correct |
9 |
Correct |
9 ms |
348 KB |
Output is correct |
10 |
Correct |
8 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
9 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
22 ms |
348 KB |
Output is correct |
15 |
Execution timed out |
2028 ms |
344 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |