답안 #895071

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895071 2023-12-29T11:34:48 Z d4xn 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);
  }
  st.insert(-1);
  st.insert(n);

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

    while (a[i] < b[i]) {
      auto l = prev(st.lower_bound(i));
      auto r = st.lower_bound(i);

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

      if (y == n || (x != -1 && d1 <= d2)) {
        int z = a[x]-b[x];
        int z2 = b[i]-a[i];
        int z3 = min(z, z2);

        a[i] += z3;
        a[x] -= z3;
        ans += z3*d1;

        if (a[x] == b[x]) st.erase(x);
      }
      else {
        int z = a[y]-b[y];
        int z2 = b[i]-a[i];
        int z3 = min(z, z2);

        a[i] += z3;
        a[y] -= z3;
        ans += z3*d2;

        if (a[y] == b[y]) st.erase(y);
      }
    }
  }
  cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 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 0 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 0 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 0 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 -