Submission #241099

#TimeUsernameProblemLanguageResultExecution timeMemory
241099aggu_01000101Coin Collecting (JOI19_ho_t4)C++14
0 / 100
5 ms384 KiB
#include <bits/stdc++.h> #define int long long #define INF 100000000 using namespace std; int n; int calcans(vector<int> &a, vector<int> &b){ int cost = 0; for(int i = n;i>=1;i--){ if(a.size() < b.size()) swap(a, b); int costa = INF, costb = INF, costc = INF; int bestcost = INF; //both from a if(a.size() >= 2){ costa = min(costa, abs(i - a[a.size()-1]) + abs(i - a[a.size()-2]) + 1); bestcost = min(bestcost, costa); } //both from b if(b.size() >= 2){ costb = min(costb, abs(i - b[b.size()-1]) + abs(i - b[b.size()-2]) + 1); bestcost = min(bestcost, costb); } //one from each if(a.size() && b.size()){ costc = min(costc, abs(i - a[a.size()-1]) + abs(i - b[b.size()-1])); bestcost = min(bestcost, costc); } if(a.size()==b.size()){ if(costc==bestcost){ a.pop_back(); b.pop_back(); } else if(costb==bestcost){ b.pop_back(); b.pop_back(); } else{ a.pop_back(); a.pop_back(); } } else{ if(costa==bestcost){ a.pop_back(); a.pop_back(); } else if(costc==bestcost){ b.pop_back(); a.pop_back(); } else{ b.pop_back(); b.pop_back(); } } cost+=bestcost; } return cost; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n; int cost = 0; vector<int> v[2]; for(int i = 0;i<(2*n);i++){ int x, y; cin>>x>>y; if(y<=1){ cost+=(1 - y); y = 1; } else{ cost+=(y - 2); y = 2; } if(x<1){ cost += (1 - x); x = 1; } if(x>n){ cost += (x - n); x = n; } //cout<<i<<" "<<x<<" "<<y<<" "<<cost<<endl; v[y-1].push_back(x); } sort(v[0].begin(), v[0].end()); sort(v[1].begin(), v[1].end()); cost+=calcans(v[0], v[1]); cout<<cost<<endl; } /* 5 1000000000 1000000000 -1000000000 1000000000 -1000000000 -1000000000 1000000000 -1000000000 -1 -5 -2 2 2 8 4 7 -2 5 7 3 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...