# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202573 | dndhk | Coin Collecting (JOI19_ho_t4) | C++14 | 5 ms | 504 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
const int MAX_N = 100000;
int cnt[MAX_N+1][3];
int N;
ll ans = 0;
int main(){
scanf("%d", &N);
for(int i=1; i<=2*N; i++){
int x, y; scanf("%d%d", &x, &y);
if(x<=1 && y<=1){
cnt[1][1]++;
ans+=(ll)(1-x+1-y);
}else if(x<=1 && y>=2){
cnt[1][2]++;
ans+=(ll)(1-x+y-2);
}else if(x>=N && y<=1){
cnt[N][1]++;
ans+=(ll)(x-N+1-y);
}else if(x>=N && y>=2){
cnt[N][2]++;
ans+=(ll)(x-N+y-2);
}else if(y<=1){
cnt[x][1]++;
ans+=(ll)(1-y);
}else{
cnt[x][2]++;
ans+=(ll)(y-2);
}
}
for(int i=1; i<=N; i++){
for(int j=1; j<=2; j++) cnt[i][j]--;
}
int idx = 1, idx2 = 1;
for(int i=1; i<=N; i++){
for(int j=1; j<=2; j++){
if(cnt[i][j]==-1){
cnt[i][j] = 0;
while(1){
//cout<<i<<" "<<j<<" "<<idx<<endl;
if(cnt[idx][j]>0){
cnt[idx][j]--;
ans+=(ll)((idx-i)>0?idx-i:i-idx);
break;
}else if(cnt[idx][3-j]>0){
cnt[idx][3-j]--;
ans+=(ll)((idx-i)>0?idx-i:i-idx);
ans+=1LL;
break;
}
idx++;
}
}
while(cnt[i][j]>0){
cnt[i][j]--;
while(1){
if(cnt[idx2][j]<0){
cnt[idx2][j]++;
ans+=(ll)((idx2-i)>0?idx2-i:i-idx2);
break;
}else if(cnt[idx2][3-j]<0){
cnt[idx2][3-j]++;
ans+=(ll)((idx2-i)>0?idx2-i:i-idx2);
ans+=1LL;
break;
}
idx2++;
}
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |