제출 #887076

#제출 시각아이디문제언어결과실행 시간메모리
887076stefanneaguCoin Collecting (JOI19_ho_t4)C++17
100 / 100
35 ms7932 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int nmax = 3e5 + 1;

int f[nmax][3];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, ans = 0;
  cin >> n;
  for(int i = 1; i <= 2 * n; i ++) {
    int a, b;
    cin >> a >> b;
    //cout << i << " ";
    // punem in cel mai apropiat loc
    if(a < 1) {
      //cout << "b mai mic ";
      ans += 1 - a;
      a = 1;
    } else if(a > n) {
      //cout << "b mai mare ";
      ans += a - n;
      a = n;
    }
    if(b < 1) {
      //cout << "a mai mic ";
      ans += 1 - b;
      b = 1;
    } else if(b > 2) {
      //cout << "a mai mare ";
      ans += b - 2;
      b = 2;
    }
    //cout << endl;
    f[a][b] ++;
  }
  int a = 0, b = 0;
  for(int i = 1; i <= n; i ++){
    a += f[i][1] - 1, b += f[i][2] - 1;
    while(a > 0 && b < 0) {
      a --;
      b ++;
      ans ++;
    }
    while(a < 0 && b > 0) {
      a ++;
      b --;
      ans ++;
    }
    ans += abs(a) + abs(b);
	}
  cout << ans;
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...