Submission #391400

#TimeUsernameProblemLanguageResultExecution timeMemory
391400timmyfengCoin Collecting (JOI19_ho_t4)C++17
0 / 100
1 ms328 KiB
#include <bits/stdc++.h> using namespace std; const int N = 100000; int coins[N][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; long long ans = 0; for (int i = 0; i < 2 * n; ++i) { int x, y; cin >> x >> y; --x, --y; if (x < 0) { ans += -x; x = 0; } else if (x >= n) { ans += x - n + 1; x = n - 1; } if (y <= 0) { ans += -y; ++coins[x][0]; } else { ans += y - 1; ++coins[x][1]; } } int k = 0; for (int i = 0; i < n; ++i) { while (coins[k][0] + coins[k][1] == 0) { ++k; } if (min(coins[k][0], coins[k][1]) > 0) { ans += 2 * abs(k - i); --coins[k][0], --coins[k][1]; } else { int j = coins[k][0] > 0; ans += abs(k - i); --coins[k][1 - j]; while (coins[k][0] + coins[k][1] == 0) { ++k; } if (coins[k][j] > 0) { ans += abs(k - i); --coins[k][j]; } else { ans += abs(k - i) + 1; --coins[k][1 - j]; } } } cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...