답안 #833258

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
833258 2023-08-22T03:47:25 Z josanneo22 Feast (NOI19_feast) C++17
63 / 100
209 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(*min_element(a.begin(), a.end())>=0){
		cout<< accumulate(a.begin(),a.end(),0LL)<<'\n';
		return;
	}
	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:59:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |      freopen("input.txt","r",stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:60:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |      freopen("output.txt","w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 5324 KB Output is correct
2 Correct 24 ms 5452 KB Output is correct
3 Correct 26 ms 5464 KB Output is correct
4 Correct 24 ms 5460 KB Output is correct
5 Correct 23 ms 5452 KB Output is correct
6 Correct 23 ms 5324 KB Output is correct
7 Correct 24 ms 5324 KB Output is correct
8 Correct 30 ms 5436 KB Output is correct
9 Correct 25 ms 5324 KB Output is correct
10 Correct 24 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 4820 KB Output is correct
2 Correct 17 ms 4988 KB Output is correct
3 Correct 15 ms 4820 KB Output is correct
4 Runtime error 183 ms 262144 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 5076 KB Output is correct
2 Correct 25 ms 4820 KB Output is correct
3 Correct 25 ms 4948 KB Output is correct
4 Correct 27 ms 4820 KB Output is correct
5 Correct 25 ms 4948 KB Output is correct
6 Correct 26 ms 4948 KB Output is correct
7 Correct 31 ms 5192 KB Output is correct
8 Correct 25 ms 4956 KB Output is correct
9 Correct 26 ms 4940 KB Output is correct
10 Correct 31 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 5 ms 3924 KB Output is correct
13 Correct 4 ms 4180 KB Output is correct
14 Correct 4 ms 4436 KB Output is correct
15 Correct 4 ms 4436 KB Output is correct
16 Correct 4 ms 3924 KB Output is correct
17 Correct 4 ms 4564 KB Output is correct
18 Correct 5 ms 4436 KB Output is correct
19 Correct 4 ms 4308 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 5 ms 3924 KB Output is correct
13 Correct 4 ms 4180 KB Output is correct
14 Correct 4 ms 4436 KB Output is correct
15 Correct 4 ms 4436 KB Output is correct
16 Correct 4 ms 3924 KB Output is correct
17 Correct 4 ms 4564 KB Output is correct
18 Correct 5 ms 4436 KB Output is correct
19 Correct 4 ms 4308 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 192 ms 207344 KB Output is correct
22 Correct 184 ms 204908 KB Output is correct
23 Correct 193 ms 208332 KB Output is correct
24 Correct 191 ms 207576 KB Output is correct
25 Correct 205 ms 219852 KB Output is correct
26 Correct 209 ms 218136 KB Output is correct
27 Correct 208 ms 218556 KB Output is correct
28 Correct 196 ms 217472 KB Output is correct
29 Correct 202 ms 213584 KB Output is correct
30 Correct 190 ms 206232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 5324 KB Output is correct
2 Correct 24 ms 5452 KB Output is correct
3 Correct 26 ms 5464 KB Output is correct
4 Correct 24 ms 5460 KB Output is correct
5 Correct 23 ms 5452 KB Output is correct
6 Correct 23 ms 5324 KB Output is correct
7 Correct 24 ms 5324 KB Output is correct
8 Correct 30 ms 5436 KB Output is correct
9 Correct 25 ms 5324 KB Output is correct
10 Correct 24 ms 5460 KB Output is correct
11 Correct 16 ms 4820 KB Output is correct
12 Correct 17 ms 4988 KB Output is correct
13 Correct 15 ms 4820 KB Output is correct
14 Runtime error 183 ms 262144 KB Execution killed with signal 9
15 Halted 0 ms 0 KB -