Submission #41328

# Submission time Handle Problem Language Result Execution time Memory
41328 2018-02-16T10:20:14 Z IvanC K blocks (IZhO14_blocks) C++14
100 / 100
249 ms 12936 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int INF = 1e9;
const int MAXN = 1e5  + 10;
int vetor[MAXN],dp[MAXN],novadp[MAXN],menor[MAXN],napilha[MAXN],N,K;
struct minStack{
	stack<ii> pilha;
	int menor;
	minStack(){menor = INF;}
	void push(int val){
		ii novo = ii(val,menor);
		menor = min(menor,val);
		pilha.push(novo);
	}
	void pop(){
		menor = pilha.top().second;
		pilha.pop();
	}
	bool empty(){return pilha.empty();}
	int getMin(){return menor;}
};
void solve(int k){
	for(int i = 0;i<=N;i++){
		novadp[i] = INF;
		napilha[i] = 0;
	}
	stack<int> pilha;
	minStack pq;
	for(int i = k;i<=N;i++){
		menor[i] = dp[i-1];
		while(!pilha.empty() && vetor[i] >= vetor[pilha.top()]){
			menor[i] = min(menor[i],menor[pilha.top()]);
			napilha[pilha.top()] = 0;
			pilha.pop();
			pq.pop();
		}
		pq.push(menor[i] + vetor[i]);
		pilha.push(i);
		napilha[i] = 1;
		if(!pq.empty()) novadp[i] = pq.getMin();
	}
	for(int i = 1;i<=N;i++) dp[i] = novadp[i];
	while(!pilha.empty()) pilha.pop();
	while(!pq.empty()) pq.pop();
}
int main(){
	scanf("%d %d",&N,&K);
	vetor[0] = dp[0] = INF;
	for(int i = 1;i<=N;i++){
		scanf("%d",&vetor[i]);
	}
	dp[1] = vetor[1];
	for(int i = 2;i<=N;i++) dp[i] = max(dp[i-1],vetor[i]);
	for(int k = 2;k<=K;k++) solve(k);
	printf("%d\n",dp[N]);
	return 0;
}

Compilation message

blocks.cpp: In function 'int main()':
blocks.cpp:48:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&N,&K);
                      ^
blocks.cpp:51:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&vetor[i]);
                        ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 2 ms 404 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 2 ms 572 KB Output is correct
6 Correct 1 ms 572 KB Output is correct
7 Correct 1 ms 572 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 620 KB Output is correct
2 Correct 1 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 620 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 2 ms 624 KB Output is correct
13 Correct 1 ms 624 KB Output is correct
14 Correct 2 ms 624 KB Output is correct
15 Correct 1 ms 624 KB Output is correct
16 Correct 2 ms 624 KB Output is correct
17 Correct 1 ms 624 KB Output is correct
18 Correct 2 ms 624 KB Output is correct
19 Correct 2 ms 736 KB Output is correct
20 Correct 2 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 736 KB Output is correct
2 Correct 1 ms 736 KB Output is correct
3 Correct 2 ms 736 KB Output is correct
4 Correct 1 ms 736 KB Output is correct
5 Correct 1 ms 736 KB Output is correct
6 Correct 2 ms 736 KB Output is correct
7 Correct 2 ms 736 KB Output is correct
8 Correct 2 ms 736 KB Output is correct
9 Correct 2 ms 736 KB Output is correct
10 Correct 1 ms 736 KB Output is correct
11 Correct 1 ms 736 KB Output is correct
12 Correct 2 ms 736 KB Output is correct
13 Correct 2 ms 736 KB Output is correct
14 Correct 2 ms 736 KB Output is correct
15 Correct 2 ms 736 KB Output is correct
16 Correct 2 ms 736 KB Output is correct
17 Correct 2 ms 736 KB Output is correct
18 Correct 2 ms 736 KB Output is correct
19 Correct 2 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 748 KB Output is correct
2 Correct 8 ms 876 KB Output is correct
3 Correct 14 ms 1388 KB Output is correct
4 Correct 71 ms 1388 KB Output is correct
5 Correct 236 ms 2540 KB Output is correct
6 Correct 26 ms 3180 KB Output is correct
7 Correct 96 ms 4044 KB Output is correct
8 Correct 2 ms 4044 KB Output is correct
9 Correct 2 ms 4044 KB Output is correct
10 Correct 2 ms 4044 KB Output is correct
11 Correct 1 ms 4044 KB Output is correct
12 Correct 2 ms 4044 KB Output is correct
13 Correct 2 ms 4044 KB Output is correct
14 Correct 23 ms 4044 KB Output is correct
15 Correct 4 ms 4044 KB Output is correct
16 Correct 8 ms 4044 KB Output is correct
17 Correct 8 ms 4044 KB Output is correct
18 Correct 14 ms 4044 KB Output is correct
19 Correct 86 ms 4156 KB Output is correct
20 Correct 249 ms 5900 KB Output is correct
21 Correct 23 ms 6512 KB Output is correct
22 Correct 118 ms 7252 KB Output is correct
23 Correct 16 ms 7252 KB Output is correct
24 Correct 44 ms 8556 KB Output is correct
25 Correct 238 ms 9388 KB Output is correct
26 Correct 2 ms 9388 KB Output is correct
27 Correct 2 ms 9388 KB Output is correct
28 Correct 23 ms 9388 KB Output is correct
29 Correct 3 ms 9388 KB Output is correct
30 Correct 8 ms 9388 KB Output is correct
31 Correct 10 ms 9388 KB Output is correct
32 Correct 15 ms 9388 KB Output is correct
33 Correct 77 ms 9580 KB Output is correct
34 Correct 245 ms 11804 KB Output is correct
35 Correct 23 ms 12212 KB Output is correct
36 Correct 113 ms 12936 KB Output is correct
37 Correct 3 ms 12936 KB Output is correct
38 Correct 24 ms 12936 KB Output is correct
39 Correct 2 ms 12936 KB Output is correct
40 Correct 2 ms 12936 KB Output is correct
41 Correct 2 ms 12936 KB Output is correct
42 Correct 2 ms 12936 KB Output is correct