Submission #707434

# Submission time Handle Problem Language Result Execution time Memory
707434 2023-03-09T02:17:32 Z safaricola Bigger segments (IZhO19_segments) C++17
100 / 100
163 ms 23692 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define rep(i,n) for(ll i = 1; i <= n; i++)
#define debug(x) cout<<#x<<' '<<x<<endl;
ll n, a[500010],kmin=LLONG_MAX,k[500010];
ii dp[500010];
int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n;
    k[1]=1;
    rep(i,n)cin>>a[i];
	dp[1]={1,a[1]};
    k[1]=1;
    dp[1]={1,a[1]};
    ll K=1,ksum=0,kmsum=0,kmmin=LLONG_MAX;
    priority_queue<ii,vector<ii>, greater<ii> > pq,bg; // stores sum in current section needed to offset the value
    pq.push({a[1],0});
    bg.push({a[1],0});
    for(int i=2; i<=n; i++){
        //make a new group
        ksum+=a[i];
        kmsum+=a[i];
        while(!bg.empty()&&bg.top().f<=ksum){
            ii cur=bg.top();
            bg.pop();
            if(bg.empty()||bg.top().f>ksum){
                bg.push(cur);
                //debug(bg.top().f)debug(bg.top().s)  debug(ksum)debug(bg.top().f)debug(bg.top().s)  debug(ksum)
                kmin=min(kmin, ksum-bg.top().s);
                break;
            }
            kmin=min(kmin, ksum-cur.s);
        }
        if(kmin!=LLONG_MAX){
            while(!bg.empty()){
                bg.pop();
            }
            while(!pq.empty()){
                pq.pop();
            }
            kmsum=0;
            for(int j=k[K]; j<i; j++){
                if(j!=k[K])kmsum+=a[j];
                //cout<<"?";debug(kmsum)
                //cout<<"pushed "<<dp[j].s+kmsum<<' '<<kmsum<<endl;
                pq.push({dp[j].s+kmsum,kmsum});
            }
            kmsum+=a[i];
            K++;
            k[K]=i;
            dp[i]={K,kmin};
            ksum=0;
            bg.push({dp[i].s,0});
            kmin=kmmin=LLONG_MAX;
            //cout<<">>"<<i<<' '<<dp[i].f<<' '<<dp[i].s<<endl;
            continue;
        }
        // get min from K-1
        dp[i]={K,LLONG_MAX};
        kmmin=LLONG_MAX;
        while(!pq.empty()&&pq.top().f<=kmsum){
            ii cur=pq.top();
            pq.pop();
            if(pq.empty()||pq.top().f>kmsum){
                pq.push(cur);
                //debug(kmsum)debug(pq.top().s)
                kmmin=min(kmmin, kmsum-pq.top().s);
                break;
            }
            kmmin=min(kmmin, kmsum-pq.top().s);
        }
        // add on to K
        dp[i].s=min({dp[i-1].s+a[i],kmmin});
        bg.push({dp[i].s+ksum,ksum});
        //cout<<"pushed "<<dp[i].s+ksum<<endl;
        //cout<<'>'<<i<<' '<<dp[i].f<<' '<<dp[i].s<<endl;
    }
	cout<<K;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 368 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 368 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 13 ms 4348 KB Output is correct
47 Correct 37 ms 3020 KB Output is correct
48 Correct 16 ms 2816 KB Output is correct
49 Correct 14 ms 3544 KB Output is correct
50 Correct 14 ms 3412 KB Output is correct
51 Correct 29 ms 2676 KB Output is correct
52 Correct 14 ms 1508 KB Output is correct
53 Correct 11 ms 1004 KB Output is correct
54 Correct 22 ms 2612 KB Output is correct
55 Correct 30 ms 2636 KB Output is correct
56 Correct 12 ms 3412 KB Output is correct
57 Correct 17 ms 5092 KB Output is correct
58 Correct 20 ms 3940 KB Output is correct
59 Correct 12 ms 3480 KB Output is correct
60 Correct 19 ms 2940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 368 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 13 ms 4348 KB Output is correct
47 Correct 37 ms 3020 KB Output is correct
48 Correct 16 ms 2816 KB Output is correct
49 Correct 14 ms 3544 KB Output is correct
50 Correct 14 ms 3412 KB Output is correct
51 Correct 29 ms 2676 KB Output is correct
52 Correct 14 ms 1508 KB Output is correct
53 Correct 11 ms 1004 KB Output is correct
54 Correct 22 ms 2612 KB Output is correct
55 Correct 30 ms 2636 KB Output is correct
56 Correct 12 ms 3412 KB Output is correct
57 Correct 17 ms 5092 KB Output is correct
58 Correct 20 ms 3940 KB Output is correct
59 Correct 12 ms 3480 KB Output is correct
60 Correct 19 ms 2940 KB Output is correct
61 Correct 47 ms 19992 KB Output is correct
62 Correct 98 ms 20812 KB Output is correct
63 Correct 163 ms 17196 KB Output is correct
64 Correct 92 ms 17304 KB Output is correct
65 Correct 79 ms 20828 KB Output is correct
66 Correct 152 ms 16864 KB Output is correct
67 Correct 119 ms 13640 KB Output is correct
68 Correct 53 ms 6952 KB Output is correct
69 Correct 115 ms 13184 KB Output is correct
70 Correct 137 ms 17000 KB Output is correct
71 Correct 56 ms 17916 KB Output is correct
72 Correct 98 ms 23692 KB Output is correct
73 Correct 80 ms 20808 KB Output is correct
74 Correct 76 ms 19148 KB Output is correct
75 Correct 78 ms 18392 KB Output is correct