Submission #393390

#TimeUsernameProblemLanguageResultExecution timeMemory
393390asbsfdsK blocks (IZhO14_blocks)C++14
100 / 100
218 ms87888 KiB
#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;
typedef long long llint;

const int maxn = 2e5+10;
const int maxk = 110;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 20;
const int off = 1 << logo;
const int treesiz = off << 1;

int n, k;
int niz[maxn];
int dp[maxk][maxn];

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) 
		scanf("%d", niz+i);
	
	memset(dp, inf, sizeof dp);
	dp[0][0] = 0;
	for (int i = 1; i <= k; i++) {
		stack< pair< pair<int, int>, int> > s;
		for (int j = 1; j <= n; j++) {
			int mini = dp[i - 1][j - 1];
			while (!s.empty() && s.top().first.first <= niz[j]) {
				mini = min(mini, s.top().first.second);
				s.pop();
			}
			
			pair<pair<int, int>, int> tren = make_pair(make_pair(niz[j], mini), niz[j] + mini);
			if (!s.empty()) {
				tren.second = min(tren.second, s.top().second);
			}
			
			s.push(tren);
			//printf("inserting: %d %d %d\n", niz[j], mini, tren.second);
			
			//printf("ave: ");
			//for (auto iter : qs) printf("%d ", iter); printf("\n");
			dp[i][j] = tren.second;
		}
	}
	
	printf("%d\n", dp[k][n]);
	return 0;
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   22 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
blocks.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   24 |   scanf("%d", niz+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...