답안 #895038

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895038 2023-12-29T11:06:43 Z vjudge1 Potatoes and fertilizers (LMIO19_bulves) C++17
0 / 100
1 ms 2396 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
 
const int N = 1e6+6;
 
int n, a[N], b[N];
set<int> st;

signed main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
 
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (a[i] > b[i]) st.insert(i);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] >= b[i]) continue;

    auto r = st.upper_bound(i);
    auto l = r;
    if (l != st.begin()) l--;

    vector<int> to_erase;
    while (a[i] < b[i]) {
      int x = *l;
      int y = (r == st.end() ? -1 : *r);

      int d1 = abs(x - i);
      int d2 = abs(y - i);

      if ((r == st.end()) || (a[y] <= b[y]) || (a[x] > b[x] && d1 <= d2)) {
        int z = a[x]-b[x];
        int z2 = b[i]-a[i];

        if (z > z2) {
          a[x] -= z2;
          ans += z2*d1;
          a[i] = b[i];
        }
        else {
          ans += z*d1;
          a[i] += z;
          a[x] = b[x];

          to_erase.push_back(x);
          if (l != st.begin()) l--;
        }
      }
      else {
        int z = a[y]-b[y];
        int z2 = b[i]-a[i];

        if (z > z2) {
          a[y] -= z2;
          ans += z2*d2;
          a[i] = b[i];
        }
        else {
          ans += z*d2;
          a[i] += z;
          a[y] = b[y];
          
          to_erase.push_back(y);
          if (r != st.end()) r++;
        }
      }
    }

    for (int& j : to_erase) st.erase(j);
  }

  cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Incorrect 1 ms 2396 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -