Submission #393008

#TimeUsernameProblemLanguageResultExecution timeMemory
393008patrikpavic2K blocks (IZhO14_blocks)C++17
100 / 100
396 ms43080 KiB
#include <cstdio>
#include <stack>
#include <cstring>

#define X first
#define Y second

using namespace std;

const int N = 1e5 + 500;
const int K = 105;
const int INF = 0x3f3f3f3f;

int dp[N][K], n, k, A[N];
stack < int > S, ja, sve;

int main(){
	memset(dp, INF, sizeof(dp)); dp[0][0] = 0;
	scanf("%d%d", &n, &k);
	for(int i = 1;i <= n;i++)
		scanf("%d", A + i);
	for(int j = 1;j <= k;j++){
		S.push(0); ja.push(dp[0][j - 1]); sve.push(dp[0][j - 1]);
		for(int i = 1;i <= n;i++){
			int bst = dp[i - 1][j - 1];
			while(!S.empty() && S.top() <= A[i]){
				bst = min(bst, ja.top()); 
				S.pop(); ja.pop(); sve.pop();
			}
			S.push(A[i]);
			ja.push(bst);
			sve.push(min(sve.empty() ? INF : sve.top(), ja.top() + S.top()));
			dp[i][j] = sve.top();
		}
		for(;!S.empty();S.pop(), ja.pop(), sve.pop()); 
	}
	printf("%d\n", dp[n][k]);
	return 0;
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   19 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
blocks.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   21 |   scanf("%d", A + i);
      |   ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...