Submission #848339

# Submission time Handle Problem Language Result Execution time Memory
848339 2023-09-12T07:38:37 Z yangjl Potatoes and fertilizers (LMIO19_bulves) C++17
100 / 100
122 ms 17120 KB
#include<iostream>
#include<cmath>
#include<cstring>
#include<cassert>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<bitset>
#include<climits>
#include<numeric>
#include<functional>
#include<iomanip>
#ifdef YJL
#include<debug.h>
#else
#define debug(args...)0
#define debug_n(a,n)0
#endif
#define ALL(a) a.begin(),a.end()
using namespace std;
using ll=long long;

constexpr ll inf = numeric_limits<ll>::max()/2;
struct slope_trick_concave {
    priority_queue<ll> ql;
    priority_queue<ll,vector<ll>,greater<>> qr;
    ll dl,dr;// 左右堆中斜率改变点的懒标记
    ll height;// 最低点高度
    slope_trick_concave(): dl(0), dr(0), height(0) {
        ql.push(-inf);
        qr.push(inf);
    }
    // g(x) = min{i = x-sub,x+add} (f(x))
    void range_min(ll sub, ll add) {
        dl-=add;
        dr+=sub;
    }
    // f(x) = g(x) + |x-c|
    void add(ll c) {
        ll xl=ql.top()+dl;
        ll xr=qr.top()+dr;
        if(xl <= c && c <= xr) {
            ql.push(c-dl);
            qr.push(c-dr);
        }else if(c < xl) {
            height+=xl-c;
            qr.push(xl-dr);
            ql.pop();
            ql.push(c-dl);
            ql.push(c-dl);
        }else {
            height+=c-xr;
            ql.push(xr-dl);
            qr.pop();
            qr.push(c-dr);
            qr.push(c-dr);
        }
    }
    void pref_min() {
        while(qr.size()>1)
            qr.pop();
    }
};

constexpr int N = 5e5 + 10;
int n;
ll c[N];

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin>>n;
    for(int i=1,x,y; i<=n; ++i) {
        cin>>x>>y;
        c[i]=c[i-1]+x-y;
    }
    // [0,c[n]]
    ll ans=0;
    slope_trick_concave sp;
    for(int i=1; i<=n; ++i) {
        if(c[i]<0) {
            ans+=-c[i];
            c[i]=0;
        }
        if(c[i]>c[n]) {
            ans+=c[i]-c[n];
            c[i]=c[n];
        }
        sp.add(c[i]);
        sp.pref_min();
    }
    cout << ans + sp.height;
    return 0;
}
/*


We want sum[] to be non-decreasing
An operation: choose i, then sum[i] += 1 or sum[i] -= 1
sum[0] must be >= 0
sum[N - 1] cannot be operated
This is classic Slope Trick

*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 6 ms 3564 KB Output is correct
5 Correct 12 ms 4568 KB Output is correct
6 Correct 34 ms 8140 KB Output is correct
7 Correct 71 ms 16588 KB Output is correct
8 Correct 58 ms 14164 KB Output is correct
9 Correct 56 ms 13868 KB Output is correct
10 Correct 47 ms 10700 KB Output is correct
11 Correct 44 ms 12492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 6 ms 3564 KB Output is correct
5 Correct 12 ms 4568 KB Output is correct
6 Correct 34 ms 8140 KB Output is correct
7 Correct 71 ms 16588 KB Output is correct
8 Correct 58 ms 14164 KB Output is correct
9 Correct 56 ms 13868 KB Output is correct
10 Correct 47 ms 10700 KB Output is correct
11 Correct 44 ms 12492 KB Output is correct
12 Correct 18 ms 5328 KB Output is correct
13 Correct 47 ms 11464 KB Output is correct
14 Correct 74 ms 16076 KB Output is correct
15 Correct 59 ms 13928 KB Output is correct
16 Correct 64 ms 14024 KB Output is correct
17 Correct 56 ms 11720 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 6 ms 3564 KB Output is correct
12 Correct 12 ms 4568 KB Output is correct
13 Correct 34 ms 8140 KB Output is correct
14 Correct 71 ms 16588 KB Output is correct
15 Correct 58 ms 14164 KB Output is correct
16 Correct 56 ms 13868 KB Output is correct
17 Correct 47 ms 10700 KB Output is correct
18 Correct 44 ms 12492 KB Output is correct
19 Correct 18 ms 5328 KB Output is correct
20 Correct 47 ms 11464 KB Output is correct
21 Correct 74 ms 16076 KB Output is correct
22 Correct 59 ms 13928 KB Output is correct
23 Correct 64 ms 14024 KB Output is correct
24 Correct 56 ms 11720 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 28 ms 5532 KB Output is correct
34 Correct 70 ms 12488 KB Output is correct
35 Correct 122 ms 16724 KB Output is correct
36 Correct 108 ms 14848 KB Output is correct
37 Correct 62 ms 14460 KB Output is correct
38 Correct 107 ms 17092 KB Output is correct
39 Correct 67 ms 13004 KB Output is correct
40 Correct 69 ms 10944 KB Output is correct
41 Correct 62 ms 10836 KB Output is correct
42 Correct 61 ms 11464 KB Output is correct
43 Correct 68 ms 11260 KB Output is correct
44 Correct 66 ms 12364 KB Output is correct
45 Correct 84 ms 17120 KB Output is correct
46 Correct 56 ms 12236 KB Output is correct
47 Correct 70 ms 12128 KB Output is correct