Submission #157626

# Submission time Handle Problem Language Result Execution time Memory
157626 2019-10-12T15:48:45 Z gemini_man K blocks (IZhO14_blocks) C++11
0 / 100
4 ms 504 KB
#include <bits/stdc++.h>
#define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i))
#define down(i,b,a) for(int (i) = (b);i>=(a);--i)
#define debug(x) cerr << (x) << '\n';
#define bits(x,i) ((x >> i) & 1)
using namespace std;
const int N = 100005;
const int K = 105;
const long long INF = 1e18;
int n,k;
long long dp[N][K];
int a[N];
int lf[N];
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	freopen("block.in","r", stdin);
	freopen("block.out","w", stdout);
	cin >> n >> k;
	up(i,1,n) cin >> a[i];
	int curMax = 0;
	up(i,0,n) up(j,0,k) dp[i][j] = INF;
	up(i,1,n){
		curMax = max(curMax, a[i]);
		dp[i][1] = curMax;
	}

	int s = 0;
	up(i,1,k){
		s += a[i];
		dp[i][i] = s;
	}
	vector<int> sk;
	up(i,1,n){
		while (!sk.empty()) {
			if (a[sk.back()] < a[i]) sk.pop_back();
			else break;
		}
		if (!sk.empty()) lf[i] = sk.back() + 1;
		else lf[i] = 1;
		sk.push_back(i); 
	}
	for( int i = 2;i <= n;++i){
		for(int j = 2; j < min(i,k+1) ;++j){
			dp[i][j] = min(dp[max(lf[i], j-1)][j-1] + a[i], dp[lf[i]-1][j]);
		}
	}
	cout << dp[n][k];
}

Compilation message

blocks.cpp: In function 'int main()':
blocks.cpp:16:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen("block.in","r", stdin);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:17:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen("block.out","w", stdout);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -