Submission #1161793

#TimeUsernameProblemLanguageResultExecution timeMemory
1161793Zero_OPCoin Collecting (JOI19_ho_t4)C++20
0 / 100
0 ms328 KiB
#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i < (r); ++i) #define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i) #define mp make_pair #define mt make_tuple #define ff first #define ss second #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) #define sz(v) (int)v.size() #define pb push_back #define eb emplace_back #define compact(v) v.erase(unique(all(v)), end(v)) #define dbg(x) "[" #x " = " << (x) << "]" template<typename T> bool minimize(T& a, const T& b){ if(a > b) return a = b, true; return false; } template<typename T> bool maximize(T& a, const T& b){ if(a < b) return a = b, true; return false; } using ll = long long; using db = double; using ld = long double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using vc = vector<char>; using vd = vector<db>; using vpi = vector<pi>; using vpl = vector<pl>; void setIO(){ ios_base::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL freopen("task.inp", "r", stdin); freopen("task.out", "w", stdout); #endif // LOCAL } ll dist(ll x1, ll y1, ll x2, ll y2){ return abs(x1 - x2) + abs(y1 - y2); } int main(){ setIO(); int N; cin >> N; vi X(2 * N), Y(2 * N); ll base = 0; FOR(i, 0, 2*N){ cin >> X[i] >> Y[i]; tuple<ll, int, int> best(1e18, 0, 0); if(1 <= X[i] && X[i] <= N){ tuple<ll, int, int> U(dist(X[i], 2, X[i], Y[i]), X[i], 2); tuple<ll, int, int> D(dist(X[i], 1, X[i], Y[i]), X[i], 1); best = min(U, D); } else{ tuple<ll, int, int> DL(dist(1, 1, X[i], Y[i]), 1, 1); tuple<ll, int, int> DR(dist(N, 1, X[i], Y[i]), N, 1); tuple<ll, int, int> UL(dist(1, 2, X[i], Y[i]), 1, 2); tuple<ll, int, int> UR(dist(N, 2, X[i], Y[i]), N, 2); best = min({DL, DR, UL, UR}); } ll cost; int x, y; tie(cost, x, y) = best; X[i] = x; Y[i] = y; base += cost; } vector<array<int, 2>> cnt(N); // cout << base << '\n'; FOR(i, 0, 2 * N){ ++cnt[X[i]-1][Y[i]-1]; } //simulate the greedy queue<int> need[2], have[2]; //only (need[0] need[1] empty) or (have[0] have[1] empty) // FOR(j, 0, 2) FOR(i, 0, N){ // cout << cnt[i][j] << " \n"[i == N-1]; // } ll simulation = 0; FOR(i, 0, N){ if(cnt[i][0] == 0) need[0].push(i); else FOR(j, 1, cnt[i][0]) have[0].push(i); if(cnt[i][1] == 0) need[1].push(i); else FOR(j, 1, cnt[i][1]) have[1].push(i); while(!need[0].empty()){ if(have[0].empty() && have[1].empty()) break; int A = (have[0].empty() ? INT_MAX : abs(have[0].front() - need[0].front())); int B = (have[1].empty() ? INT_MAX : abs(have[1].front() - need[0].front()) + 1); if(A <= B){ // cout << dbg(0) << dbg(need[0].front()) << dbg(have[0].front()) << '\n'; simulation += A; have[0].pop(); } else{ // cout << dbg(0) << dbg(need[0].front()) << dbg(have[1].front()) << '\n'; simulation += B; have[1].pop(); } need[0].pop(); } while(!need[1].empty()){ if(have[0].empty() && have[1].empty()) break; int A = (have[1].empty() ? INT_MAX : abs(have[1].front() - need[1].front())); int B = (have[0].empty() ? INT_MAX : abs(have[0].front() - need[1].front()) + 1); if(A <= B){ // cout << dbg(1) << dbg(need[1].front()) << dbg(have[1].front()) << '\n'; simulation += A; have[1].pop(); } else{ // cout << dbg(1) << dbg(need[1].front()) << dbg(have[0].front()) << '\n'; simulation += B; have[0].pop(); } need[1].pop(); } } FOR(i, 0, 2){ assert(need[i].empty()); assert(have[i].empty()); } cout << base + simulation << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...