Submission #865883

#TimeUsernameProblemLanguageResultExecution timeMemory
865883phoenix0423Coin Collecting (JOI19_ho_t4)C++17
100 / 100
33 ms5808 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pll; #define fastio ios::sync_with_stdio(false), cin.tie(0) // #pragma GCC optimize("Ofast") #define pb push_back #define eb emplace_back #define f first #define s second #define lowbit(x) x&-x const int maxn = 1e5 + 5; const int INF = 1e9; const double eps = 1e-7; int cnt[maxn][2]; int main(void){ fastio; int n; cin>>n; ll ans = 0; for(int i = 0; i < 2 * n; i++){ int x, y; cin>>x>>y; x--, y--; if(y >= 1) ans += y - 1, y = 1; else ans += -y, y = 0; if(x >= n - 1) ans += x - n + 1, x = n - 1; else if(x < 0) ans += -x, x = 0; cnt[x][y]++; } for(int j = 0; j < 2; j++){ for(int i = 0; i < n; i++) cnt[i][j] -= 1; } vector<int> dist(2), cur(2); //see operations as moving boxed for(int i = 0; i < n; i++){ ans += abs(cur[0]) + abs(cur[1]); cur[0] += cnt[i][0], cur[1] += cnt[i][1]; if(cur[0] > 0 && cur[1] < 0){ int trade = min(cur[0], -cur[1]); ans += trade; cur[0] -= trade, cur[1] += trade; } if(cur[1] > 0 && cur[0] < 0){ int trade = min(-cur[0], cur[1]); ans += trade; cur[0] += trade, cur[1] -= trade; } } cout<<ans<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...