Submission #833256

# Submission time Handle Problem Language Result Execution time Memory
833256 2023-08-22T03:46:17 Z josanneo22 Feast (NOI19_feast) C++17
59 / 100
237 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
const int inf=1e15;
long long maxSubArraySum(vector<long long> a, int size)
{
	long long max_so_far = LLONG_MIN, max_ending_here = 0;
	int starting_index = 0, ending_index = 0, s = 0;
	for (int i = 0; i < size; i++) {
		max_ending_here = max_ending_here + a[i];
		if (max_so_far < max_ending_here) {
			max_so_far = max_ending_here; starting_index = s; ending_index = i;
		}
 
		if (max_ending_here < 0) {
			max_ending_here = 0;
			s = i + 1;
		}
	}
	return max_so_far;
}
void solve(){
	int n,k; cin>>n>>k;
	vector<int> a(n+1); for(int i=1;i<=n;i++) cin>>a[i];
	if(k==1){
		cout<<max(0LL,maxSubArraySum(a,n+1))<<'\n';
		return;
	}
	
	vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1,vector<int>(2,-inf)));
	dp[0][0][0]=0; dp[0][0][1]=0; int ans=0;
	for(int i=1;i<=n;i++){
		dp[i][0][0]=0;
		for(int j=1;j<=n;j++){
			dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);
			dp[i][j][1]=max(dp[i-1][j][1],dp[i-1][j-1][0])+a[i];
			if(j<=k) ans=max(ans,max(dp[i][j][0],dp[i][j][1]));
		}
	}
	cout<<ans<<'\n';
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	clock_t tStart = clock();
	int local=0,multi=0,debug=1,tt=1;
	if(local){
	    freopen("input.txt","r",stdin);
	    freopen("output.txt","w",stdout);
	}	
	if(multi) cin>>tt;
	for(int i=1;i<=tt;i++){
		if(debug && multi && local) cout<<"样例 "<<i<<'\n';
		solve();
 	}
	fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}

Compilation message

feast.cpp: In function 'long long int maxSubArraySum(std::vector<long long int>, long long int)':
feast.cpp:16:6: warning: variable 'starting_index' set but not used [-Wunused-but-set-variable]
   16 |  int starting_index = 0, ending_index = 0, s = 0;
      |      ^~~~~~~~~~~~~~
feast.cpp:16:26: warning: variable 'ending_index' set but not used [-Wunused-but-set-variable]
   16 |  int starting_index = 0, ending_index = 0, s = 0;
      |                          ^~~~~~~~~~~~
feast.cpp: In function 'int main()':
feast.cpp:56:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |      freopen("input.txt","r",stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:57:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |      freopen("output.txt","w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 199 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4800 KB Output is correct
2 Correct 21 ms 6092 KB Output is correct
3 Correct 16 ms 5932 KB Output is correct
4 Runtime error 180 ms 262144 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 4960 KB Output is correct
2 Correct 27 ms 7796 KB Output is correct
3 Correct 27 ms 7860 KB Output is correct
4 Correct 27 ms 7836 KB Output is correct
5 Correct 28 ms 7836 KB Output is correct
6 Correct 27 ms 8008 KB Output is correct
7 Correct 27 ms 7884 KB Output is correct
8 Correct 27 ms 7828 KB Output is correct
9 Correct 27 ms 7916 KB Output is correct
10 Correct 27 ms 7896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 3 ms 3924 KB Output is correct
13 Correct 3 ms 4052 KB Output is correct
14 Correct 3 ms 4436 KB Output is correct
15 Correct 4 ms 4436 KB Output is correct
16 Correct 3 ms 3924 KB Output is correct
17 Correct 4 ms 4644 KB Output is correct
18 Correct 4 ms 4364 KB Output is correct
19 Correct 4 ms 4308 KB Output is correct
20 Correct 4 ms 5116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 3 ms 3924 KB Output is correct
13 Correct 3 ms 4052 KB Output is correct
14 Correct 3 ms 4436 KB Output is correct
15 Correct 4 ms 4436 KB Output is correct
16 Correct 3 ms 3924 KB Output is correct
17 Correct 4 ms 4644 KB Output is correct
18 Correct 4 ms 4364 KB Output is correct
19 Correct 4 ms 4308 KB Output is correct
20 Correct 4 ms 5116 KB Output is correct
21 Correct 192 ms 207392 KB Output is correct
22 Correct 187 ms 204956 KB Output is correct
23 Correct 199 ms 208360 KB Output is correct
24 Correct 188 ms 207564 KB Output is correct
25 Correct 208 ms 219848 KB Output is correct
26 Correct 227 ms 218108 KB Output is correct
27 Correct 195 ms 218544 KB Output is correct
28 Correct 201 ms 217496 KB Output is correct
29 Correct 237 ms 213568 KB Output is correct
30 Correct 191 ms 206204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 199 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -