Submission #224532

# Submission time Handle Problem Language Result Execution time Memory
224532 2020-04-18T10:35:17 Z kshitij_sodani Feast (NOI19_feast) C++17
71 / 100
230 ms 120168 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long int llo;
llo mod=1000000007;
llo dp[2001][2001];
llo cost[2001][2001];
llo ma=0;
llo dnc(llo ind,llo l,llo r,llo ll,llo rr){
	llo mid=(l+r)/2;
	pair<llo,llo> best={-1,-1};
	for(llo i=ll;i<=rr;i++){
		if(i>mid){
			continue;
		}
		if(i==ll){
			if(i==0){
				best={-cost[0][mid],i};
			}
			else{
				best={-dp[i-1][ind-1]-cost[i][mid],i};
			}
		}

		else{
			best=min(best,{-dp[i-1][ind-1]-cost[i][mid],i});
		}
	}
	
	dp[mid][ind]=-best.a;

	ma=max(ma,dp[mid][ind]);
	if(mid>l){
		dnc(ind,l,mid-1,ll,best.b);
	}
	if(mid<r){
		dnc(ind,mid+1,r,best.b,rr);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	llo n,k;
	cin>>n>>k;

	llo it[n];
	for(llo i=0;i<n;i++){
		cin>>it[i];
	}
	if(k==1){
		llo su=0;
		for(int i=0;i<n;i++){
			su+=it[i];
			su=max(su,(llo)0);
			ma=max(ma,su);
		}
		cout<<ma<<endl;
		return 0;
	}
	llo ind=0;
	vector<llo> aa;
	while(ind<n){
		if(it[ind]<=0){
			llo su=0;
			while(ind<n){
				if(it[ind]>0){
					break;
				}
				su+=it[ind];
				ind+=1;
			}
			aa.pb(su);
		}
		if(it[ind]>0){
			llo su=0;
			while(ind<n){
				if(it[ind]<0){
					break;
				}
				su+=it[ind];
				ind+=1;
			}
			aa.pb(su);
		}
	}
	llo j=0;
	for(llo i=0;i<aa.size();i++){
		if(aa[i]>0){
			llo ma=0;
			llo su=0;
			llo ind=i;
			llo ind2=j;
			while(ind>=0){
				su+=aa[ind];
				ma=max(ma,su);
				if(aa[ind]>0){
					cost[ind2][j]=ma;
					ind2-=1;
				}
				ind-=1;
			}	
			j+=1;
		}
	}
	for(llo i=0;i<j;i++){
		dp[i][0]=cost[0][i];
		ma=max(ma,cost[0][i]);
	}
	for(llo jj=1;jj<min(j,k);jj++){
		dnc(jj,0,j-1,0,j-1);

	}
	cout<<ma<<endl;













	return 0;
}

Compilation message

feast.cpp: In function 'llo dnc(llo, llo, llo, llo, llo)':
feast.cpp:42:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
feast.cpp: In function 'int main()':
feast.cpp:90:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(llo i=0;i<aa.size();i++){
              ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 43 ms 2560 KB Output is correct
2 Correct 45 ms 2680 KB Output is correct
3 Correct 46 ms 2680 KB Output is correct
4 Correct 44 ms 2680 KB Output is correct
5 Correct 44 ms 2680 KB Output is correct
6 Correct 43 ms 2552 KB Output is correct
7 Correct 45 ms 2680 KB Output is correct
8 Correct 44 ms 2680 KB Output is correct
9 Correct 46 ms 2552 KB Output is correct
10 Correct 44 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2552 KB Output is correct
2 Correct 27 ms 2688 KB Output is correct
3 Correct 29 ms 2552 KB Output is correct
4 Correct 28 ms 2680 KB Output is correct
5 Correct 48 ms 2680 KB Output is correct
6 Correct 26 ms 2560 KB Output is correct
7 Correct 26 ms 2688 KB Output is correct
8 Correct 46 ms 2680 KB Output is correct
9 Correct 45 ms 2552 KB Output is correct
10 Correct 27 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 2680 KB Output is correct
2 Correct 55 ms 5624 KB Output is correct
3 Correct 50 ms 5624 KB Output is correct
4 Correct 60 ms 5656 KB Output is correct
5 Correct 49 ms 5624 KB Output is correct
6 Correct 52 ms 5752 KB Output is correct
7 Correct 55 ms 5752 KB Output is correct
8 Correct 50 ms 5624 KB Output is correct
9 Correct 51 ms 5752 KB Output is correct
10 Correct 48 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 5 ms 896 KB Output is correct
12 Correct 5 ms 896 KB Output is correct
13 Correct 5 ms 896 KB Output is correct
14 Correct 5 ms 1024 KB Output is correct
15 Correct 5 ms 896 KB Output is correct
16 Correct 5 ms 896 KB Output is correct
17 Correct 5 ms 1024 KB Output is correct
18 Correct 5 ms 1024 KB Output is correct
19 Correct 5 ms 896 KB Output is correct
20 Correct 5 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 5 ms 896 KB Output is correct
12 Correct 5 ms 896 KB Output is correct
13 Correct 5 ms 896 KB Output is correct
14 Correct 5 ms 1024 KB Output is correct
15 Correct 5 ms 896 KB Output is correct
16 Correct 5 ms 896 KB Output is correct
17 Correct 5 ms 1024 KB Output is correct
18 Correct 5 ms 1024 KB Output is correct
19 Correct 5 ms 896 KB Output is correct
20 Correct 5 ms 896 KB Output is correct
21 Correct 12 ms 5888 KB Output is correct
22 Correct 15 ms 7168 KB Output is correct
23 Correct 14 ms 6656 KB Output is correct
24 Correct 11 ms 6144 KB Output is correct
25 Correct 14 ms 6784 KB Output is correct
26 Correct 12 ms 6016 KB Output is correct
27 Correct 14 ms 6528 KB Output is correct
28 Correct 8 ms 5504 KB Output is correct
29 Correct 9 ms 5248 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 2560 KB Output is correct
2 Correct 45 ms 2680 KB Output is correct
3 Correct 46 ms 2680 KB Output is correct
4 Correct 44 ms 2680 KB Output is correct
5 Correct 44 ms 2680 KB Output is correct
6 Correct 43 ms 2552 KB Output is correct
7 Correct 45 ms 2680 KB Output is correct
8 Correct 44 ms 2680 KB Output is correct
9 Correct 46 ms 2552 KB Output is correct
10 Correct 44 ms 2680 KB Output is correct
11 Correct 27 ms 2552 KB Output is correct
12 Correct 27 ms 2688 KB Output is correct
13 Correct 29 ms 2552 KB Output is correct
14 Correct 28 ms 2680 KB Output is correct
15 Correct 48 ms 2680 KB Output is correct
16 Correct 26 ms 2560 KB Output is correct
17 Correct 26 ms 2688 KB Output is correct
18 Correct 46 ms 2680 KB Output is correct
19 Correct 45 ms 2552 KB Output is correct
20 Correct 27 ms 2688 KB Output is correct
21 Correct 50 ms 2680 KB Output is correct
22 Correct 55 ms 5624 KB Output is correct
23 Correct 50 ms 5624 KB Output is correct
24 Correct 60 ms 5656 KB Output is correct
25 Correct 49 ms 5624 KB Output is correct
26 Correct 52 ms 5752 KB Output is correct
27 Correct 55 ms 5752 KB Output is correct
28 Correct 50 ms 5624 KB Output is correct
29 Correct 51 ms 5752 KB Output is correct
30 Correct 48 ms 5752 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 4 ms 512 KB Output is correct
33 Correct 5 ms 512 KB Output is correct
34 Correct 4 ms 512 KB Output is correct
35 Correct 4 ms 512 KB Output is correct
36 Correct 5 ms 512 KB Output is correct
37 Correct 4 ms 512 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 5 ms 384 KB Output is correct
40 Correct 4 ms 512 KB Output is correct
41 Correct 5 ms 896 KB Output is correct
42 Correct 5 ms 896 KB Output is correct
43 Correct 5 ms 896 KB Output is correct
44 Correct 5 ms 1024 KB Output is correct
45 Correct 5 ms 896 KB Output is correct
46 Correct 5 ms 896 KB Output is correct
47 Correct 5 ms 1024 KB Output is correct
48 Correct 5 ms 1024 KB Output is correct
49 Correct 5 ms 896 KB Output is correct
50 Correct 5 ms 896 KB Output is correct
51 Correct 12 ms 5888 KB Output is correct
52 Correct 15 ms 7168 KB Output is correct
53 Correct 14 ms 6656 KB Output is correct
54 Correct 11 ms 6144 KB Output is correct
55 Correct 14 ms 6784 KB Output is correct
56 Correct 12 ms 6016 KB Output is correct
57 Correct 14 ms 6528 KB Output is correct
58 Correct 8 ms 5504 KB Output is correct
59 Correct 9 ms 5248 KB Output is correct
60 Correct 7 ms 5120 KB Output is correct
61 Runtime error 230 ms 120168 KB Execution killed with signal 11 (could be triggered by violating memory limits)
62 Halted 0 ms 0 KB -