답안 #851299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
851299 2023-09-19T12:47:17 Z yangjl Potatoes and fertilizers (LMIO19_bulves) C++17
100 / 100
179 ms 26836 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int,int>;

template< typename T >
struct SlopeTrick {
  const T INF = numeric_limits< T >::max() / 3;
  T min_f;
  priority_queue< T, vector< T >, less<> > L;
  priority_queue< T, vector< T >, greater<> > R;
  T add_l, add_r;
  T imag_l, imag_r;
private:
  void push_R(const T &a) {
    R.push(a - add_r);
  }
  T top_R() const {
    if(R.empty()) return INF;
    else return R.top() + add_r;
  }
  T pop_R() {
    T val = top_R();
    if(not R.empty()) R.pop();
    return val;
  }
  void push_L(const T &a) {
    L.push(a - add_l);
  }
  T top_L() {
    L.push(0);
    if(L.empty()) return -INF;
    else {
      return L.top() + add_l;
    }
  }
  T pop_L() {
    T val = top_L();
    if(not L.empty()) L.pop();
    return val;
  }
  size_t size() {
    return L.size() + R.size();
  }
public:
  SlopeTrick() : min_f(0), add_l(0), add_r(0), imag_l(1ll<<60), imag_r(1ll<<60) {}
  struct Query {
    T lx, rx, min_f;
  };
  // return min f(x)
  Query query() const {
    return (Query) {top_L(), top_R(), min_f};
  }
  // f(x) += a
  void add_all(const T &a) {
    min_f += a;
  }
  // add \_
  // f(x) += max(a - x, 0)
  void add_a_minus_x(const T &a) {
    min_f += max(T(0), a - top_R());
    push_R(a);
    push_L(pop_R());
  }
  // add _/
  // f(x) += max(x - a, 0)
  void add_x_minus_a(const T &a) {
    min_f += max(T(0), top_L() - a);
    push_L(a);
    push_R(pop_L());
  }
  // add \/
  // f(x) += abs(x - a)
  void add_abs(const T &a) {
    add_a_minus_x(a);
    add_x_minus_a(a);
  }
  // \/ -> \_
  // f_{new} (x) = min f(y) (y <= x)
  void clear_right() {
    while(not R.empty()) R.pop();
    imag_r = 0;
  }
  // \/ -> _/
  // f_{new} (x) = min f(y) (y >= x)
  void clear_left() {
    while(not L.empty()) L.pop();
  }
  // \/ -> \_/
  // f_{new} (x) = min f(y) (x-b <= y <= x-a)
  void shift(const T &a, const T &b) {
    assert(a <= b);
    add_l += a;
    add_r += b;
  }
  // \/. -> .\/
  // f_{new} (x) = f(x - a)
  void shift(const T &a) {
    shift(a, a);
  }
  // L, R 被破坏
  T get(const T &x) {
    T ret = min_f;
    while(not L.empty()) {
      auto val = pop_L();
      ret += max(T(0), val - x);
      if(val == add_l) break;
    }
    // while(not R.empty()) {
    //   ret += max(T(0), x - pop_R());
    // }
    return ret;
  }
  void merge(SlopeTrick &st) {
    if(st.size() > size()) {
      swap(st.L, L);
      swap(st.R, R);
      swap(st.add_l, add_l);
      swap(st.add_r, add_r);
      swap(st.min_f, min_f);
    }
    while(not st.R.empty()) {
      add_x_minus_a(st.pop_R());
    }
    while(not st.L.empty()) {
      add_a_minus_x(st.pop_L());
    }
    min_f += st.min_f;
  }

  void print(){
    cout << "L:";
    while(!L.empty()) cout << " " << pop_L(); cout << '\n';
    cout << "R:";
    while(!R.empty()) cout << " " << pop_R(); cout << '\n';
  }
};

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for(int i = 0; i < n; i++) cin >> a[i]>> b[i];
  for(int i = 1; i < n; i++) a[i] += a[i - 1];
  SlopeTrick<ll> pq;
  for(int i = 0; i < n; i++){
    pq.shift(b[i]);
    pq.clear_right();
    pq.add_abs(a[i]);
  }
  cout << pq.get(a[n - 1]) << '\n';
  return 0;
}

Compilation message

bulves.cpp: In member function 'void SlopeTrick<T>::print()':
bulves.cpp:134:5: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  134 |     while(!L.empty()) cout << " " << pop_L(); cout << '\n';
      |     ^~~~~
bulves.cpp:134:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  134 |     while(!L.empty()) cout << " " << pop_L(); cout << '\n';
      |                                               ^~~~
bulves.cpp:136:5: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  136 |     while(!R.empty()) cout << " " << pop_R(); cout << '\n';
      |     ^~~~~
bulves.cpp:136:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  136 |     while(!R.empty()) cout << " " << pop_R(); cout << '\n';
      |                                               ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 13 ms 3288 KB Output is correct
5 Correct 27 ms 7880 KB Output is correct
6 Correct 80 ms 12944 KB Output is correct
7 Correct 113 ms 26040 KB Output is correct
8 Correct 144 ms 26460 KB Output is correct
9 Correct 121 ms 26428 KB Output is correct
10 Correct 95 ms 26576 KB Output is correct
11 Correct 102 ms 24772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 13 ms 3288 KB Output is correct
5 Correct 27 ms 7880 KB Output is correct
6 Correct 80 ms 12944 KB Output is correct
7 Correct 113 ms 26040 KB Output is correct
8 Correct 144 ms 26460 KB Output is correct
9 Correct 121 ms 26428 KB Output is correct
10 Correct 95 ms 26576 KB Output is correct
11 Correct 102 ms 24772 KB Output is correct
12 Correct 41 ms 7624 KB Output is correct
13 Correct 100 ms 13420 KB Output is correct
14 Correct 119 ms 25024 KB Output is correct
15 Correct 143 ms 24772 KB Output is correct
16 Correct 164 ms 26532 KB Output is correct
17 Correct 89 ms 25920 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 13 ms 3288 KB Output is correct
12 Correct 27 ms 7880 KB Output is correct
13 Correct 80 ms 12944 KB Output is correct
14 Correct 113 ms 26040 KB Output is correct
15 Correct 144 ms 26460 KB Output is correct
16 Correct 121 ms 26428 KB Output is correct
17 Correct 95 ms 26576 KB Output is correct
18 Correct 102 ms 24772 KB Output is correct
19 Correct 41 ms 7624 KB Output is correct
20 Correct 100 ms 13420 KB Output is correct
21 Correct 119 ms 25024 KB Output is correct
22 Correct 143 ms 24772 KB Output is correct
23 Correct 164 ms 26532 KB Output is correct
24 Correct 89 ms 25920 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 41 ms 8112 KB Output is correct
34 Correct 101 ms 13848 KB Output is correct
35 Correct 173 ms 26836 KB Output is correct
36 Correct 179 ms 25792 KB Output is correct
37 Correct 146 ms 24764 KB Output is correct
38 Correct 173 ms 24768 KB Output is correct
39 Correct 132 ms 24812 KB Output is correct
40 Correct 124 ms 25684 KB Output is correct
41 Correct 107 ms 26300 KB Output is correct
42 Correct 107 ms 26052 KB Output is correct
43 Correct 100 ms 26044 KB Output is correct
44 Correct 100 ms 26044 KB Output is correct
45 Correct 155 ms 25772 KB Output is correct
46 Correct 138 ms 25784 KB Output is correct
47 Correct 120 ms 24072 KB Output is correct