제출 #590753

#제출 시각아이디문제언어결과실행 시간메모리
590753MilosMilutinovicCoin Collecting (JOI19_ho_t4)C++14
100 / 100
87 ms11984 KiB
/**
 *    author:  wxhtzdy
 *    created: 06.07.2022 12:06:17
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<int> x(2 * n), y(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    cin >> x[i] >> y[i];
  }
  long long ans = 0;
  vector<vector<int>> cnt(n, vector<int>(2));
  for (int i = 0; i < 2 * n; i++) {
    if (x[i] < 1) {
      ans += 1 - x[i];
      x[i] = 1;
    }
    if (y[i] < 1) {
      ans += 1 - y[i];
      y[i] = 1;
    }
    if (x[i] > n) {
      ans += x[i] - n;
      x[i] = n;
    }
    if (y[i] > 2) {
      ans += y[i] - 2;
      y[i] = 2;  
    }
    cnt[x[i] - 1][y[i] - 1] += 1;
  }
  int L = 0, R = 0;
  for (int i = 0; i < n; i++) {
    L += cnt[i][0] - 1;
    R += cnt[i][1] - 1;
    while (L < 0 && R > 0) {
      ans += 1;
      L += 1;
      R -= 1;
    }
    while (R < 0 && L > 0) {
      ans += 1;
      R += 1;
      L -= 1;
    }       
    ans += abs(L) + abs(R);
  }          
  cout << ans << '\n';                                  
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...