Submission #883239

#TimeUsernameProblemLanguageResultExecution timeMemory
883239SalihSahinCoin Collecting (JOI19_ho_t4)C++14
100 / 100
53 ms14024 KiB
#include<bits/stdc++.h> #define pb push_back #define int long long #define mp make_pair using namespace std; const int inf = 2e18; const int mod = 1e9 + 7; const int N = 1e5 + 5; int hizalay(int &y){ if(y >= 2){ int k = y - 2; y = 2; return k; } else{ int k = 1 - y; y = 1; return k; } } int hizalax(int &x, int n){ if(x >= n){ int k = x - n; x = n; return k; } else if(x <= 1){ int k = 1 - x; x = 1; return k; } else return 0; } int32_t main(){ ios_base::sync_with_stdio(false), cin.tie(); int n; cin>>n; vector<pair<int, int> > a(n*2); vector<vector<int> > grid(n+1, vector<int>(2)); int ans = 0, sz; for(int i = 0; i < 2*n; i++){ cin>>a[i].first>>a[i].second; ans += hizalay(a[i].second); ans += hizalax(a[i].first, n); a[i].first--, a[i].second--; grid[a[i].first][a[i].second]++; } vector<int> rem[2]; for(int i = 0; i < n; i++){ int cnt = min((int)(rem[1].size()), grid[i][1]); sz = rem[1].size(); for(int j = sz-1; j >= sz - cnt; j--){ ans += (i - rem[1][j]); rem[1].pop_back(); } grid[i][1] -= cnt; cnt = min((int)(rem[0].size()), grid[i][0]); sz = rem[0].size(); for(int j = sz-1; j >= sz - cnt; j--){ ans += (i - rem[0][j]); rem[0].pop_back(); } grid[i][0] -= cnt; if(rem[0].size() && grid[i][1]){ cnt = min((int)(rem[0].size()), grid[i][1] - 1); sz = rem[0].size(); for(int j = sz-1; j >= sz - cnt; j--){ ans += (i - rem[0][j] + 1); rem[0].pop_back(); } grid[i][1] -= cnt; } if(rem[1].size() && grid[i][0]){ cnt = min((int)(rem[1].size()), grid[i][0] - 1); sz = rem[1].size(); for(int j = sz-1; j >= sz - cnt; j--){ ans += (i - rem[1][j] + 1); rem[1].pop_back(); } grid[i][0] -= cnt; } if(grid[i][1] > 1 && !grid[i][0]){ ans++; grid[i][1]--; grid[i][0]++; } if(!grid[i][1] && grid[i][0] > 1){ ans++; grid[i][0]--; grid[i][1]++; } if(grid[i][1] > 1){ int ver = grid[i][1] - 1; grid[i+1][1] += ver; grid[i][1] -= ver; ans += ver; } if(grid[i][0] > 1){ int ver = grid[i][0] - 1; grid[i+1][0] += ver; grid[i][0] -= ver; ans += ver; } if(!grid[i][1]) rem[1].pb(i); if(!grid[i][0]) rem[0].pb(i); } cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...