Submission #776101

#TimeUsernameProblemLanguageResultExecution timeMemory
776101gun_ganCoin Collecting (JOI19_ho_t4)C++17
100 / 100
39 ms5864 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 2e5 + 7;

ll N;
ll a[MX][3], b[MX];
ll ans = 0;
 
int main() {
      cin.tie(0); ios_base::sync_with_stdio(0);
 
      cin >> N;
      for(int i = 0; i < 2 * N; i++) {
            ll x, y;
            cin >> x >> y;

            if(1 <= x && x <= N && 1 <= y && y <= 2) {
                  a[x][y]++;
            } else if(1 <= x && x <= N) {
                  if(y > 2) {
                        a[x][2]++;
                        ans += y - 2;
                  } else {
                        a[x][1]++;
                        ans += 1 - y;
                  }
            } else if(1 <= y && y <= 2) {
                  if(x < 1) {
                        a[1][y]++;
                        ans += 1 - x;
                  } else {
                        a[N][y]++;
                        ans += x - N;
                  }
            } else {
                  if(y > 2 && x < 1) {
                        a[1][2]++;
                        ans += 1 - x + y - 2;
                  } else if(y > 2 && x > N) {
                        a[N][2]++;
                        ans += x - N + y - 2;
                  } else if(y < 1 && x < 1) {
                        a[1][1]++;
                        ans += 1 - x + 1 - y;
                  } else {
                        a[N][1]++;
                        ans += x - N + 1 - y;
                  }
            }
      }

      int sum1 = 0, sum2 = 0;
      for(int i = 1; i <= N; i++) {
            sum1 += a[i][1] - 1;
            sum2 += a[i][2] - 1;

            if(sum1 > 0 && sum2 < 0) {
                  int t = min(sum1, -sum2);
                  ans += t;
                  sum1 -= t;
                  sum2 += t;
            }

            if(sum1 < 0 && sum2 > 0) {
                  int t = min(-sum1, sum2);
                  ans += t;
                  sum1 += t;
                  sum2 -= t;
            }

            ans += abs(sum1) + abs(sum2);
      }     

      // for(int i = 2; i >= 1; i--) {
      //       for(int j = 1; j <= N; j++) {
      //             cout << a[j][i] << " ";
      //       }
      //       cout << '\n';
      // }

      cout << ans << '\n';
      
}     
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...