Submission #860982

#TimeUsernameProblemLanguageResultExecution timeMemory
860982NeroZeinCoin Collecting (JOI19_ho_t4)C++17
100 / 100
51 ms12000 KiB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
 
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n; 
  vector<int> x(n * 2), y(n * 2);
  for (int i = 0; i < n * 2; ++i) {
    cin >> x[i] >> y[i];
  }
  long long ans = 0;
  vector<vector<int>> p(n + 1, vector<int> (3)); 
  for (int i = 0; i < n * 2; ++i) {
    int nxtx = 0, nxty = 0;
    if (1 <= x[i] && x[i] <= n) {
      nxtx = x[i];
    } else if (x[i] < 1) {
      nxtx = 1;
    } else {
      nxtx = n; 
    }
    if (y[i] <= 1) {
      nxty = 1;
    } else {
      nxty = 2;
    }
    ans += abs(x[i] - nxtx);
    ans += abs(y[i] - nxty); 
    p[nxtx][nxty]++; 
  }
  int up = 0, down = 0;
  for (int i = 1; i <= n; ++i) {
    up += p[i][2] - 1;
    down += p[i][1] - 1; 
    if (up > 0 && down < 0) {
      int t = min(up, -down);
      ans += t;
      up -= t;
      down += t; 
    } else if (up < 0 && down > 0) {
      int t = min(-up, down);
      ans += t;
      up += t;
      down -= t;
    }
    ans += abs(up) + abs(down); //one step to the right
  }
  cout << ans << '\n'; 
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...