Submission #114320

# Submission time Handle Problem Language Result Execution time Memory
114320 2019-05-31T21:03:12 Z MohamedAhmed04 K blocks (IZhO14_blocks) C++14
100 / 100
694 ms 80760 KB
#include <bits/stdc++.h>
 
using namespace std ;
 
stack<int>s ;
 
int main()
{
	int n , k ;
	scanf("%d %d" , &n , &k) ;
	int arr[n+1] ;
	for(int i = 1 ; i <= n ; ++i)
		scanf("%d" , &arr[i]) ;
	int dp[n+1][k+1] ;
	int chosen[n+1][k+1] ;
	dp[0][1] = 0 ;
	for(int i = 1 ; i <= n ; ++i)
		dp[i][1] = max(arr[i] , dp[i-1][1]) , chosen[i][1] = dp[i][1];
	for(int j = 2 ; j <= k ; ++j)
	{
      	while(s.size() > 0)
          s.pop() ;
		for(int i = j ; i <= n ; ++i)
		{
			dp[i][j] = dp[i-1][j-1] + arr[i] ;
			chosen[i][j] = arr[i] ;
			while(s.size() > 0)
			{
				int idx = s.top() ;
				if(arr[idx] <= arr[i])
				{
					if(dp[idx][j] - chosen[idx][j] + max(chosen[idx][j] , arr[i]) < dp[i][j])
					{
						dp[i][j] = dp[idx][j] - chosen[idx][j] + max(chosen[idx][j] , arr[i]) ;
					}
					s.pop() ;
				}
				else
					break ;
			}
			if(s.size() > 0)
			{
				int idx = s.top() ;
				if(dp[idx][j] < dp[i][j])
				{
					dp[i][j] = dp[idx][j] ;
                    chosen[i][j] = chosen[idx][j] ;
				}
			}
			s.push(i) ;
		}
	}
	printf("%d" , dp[n][k]) ;
	return 0 ;
}

Compilation message

blocks.cpp: In function 'int main()':
blocks.cpp:10:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d" , &n , &k) ;
  ~~~~~^~~~~~~~~~~~~~~~~~~
blocks.cpp:13:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d" , &arr[i]) ;
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2432 KB Output is correct
2 Correct 8 ms 1536 KB Output is correct
3 Correct 13 ms 3584 KB Output is correct
4 Correct 107 ms 27640 KB Output is correct
5 Correct 596 ms 80632 KB Output is correct
6 Correct 19 ms 4480 KB Output is correct
7 Correct 243 ms 37456 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 39 ms 8320 KB Output is correct
15 Correct 4 ms 640 KB Output is correct
16 Correct 10 ms 2432 KB Output is correct
17 Correct 8 ms 1536 KB Output is correct
18 Correct 15 ms 3584 KB Output is correct
19 Correct 144 ms 27632 KB Output is correct
20 Correct 694 ms 80504 KB Output is correct
21 Correct 23 ms 4608 KB Output is correct
22 Correct 261 ms 37496 KB Output is correct
23 Correct 15 ms 2936 KB Output is correct
24 Correct 41 ms 9984 KB Output is correct
25 Correct 645 ms 80544 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 34 ms 8320 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 9 ms 2432 KB Output is correct
31 Correct 8 ms 1536 KB Output is correct
32 Correct 14 ms 3584 KB Output is correct
33 Correct 144 ms 27708 KB Output is correct
34 Correct 624 ms 80760 KB Output is correct
35 Correct 19 ms 4736 KB Output is correct
36 Correct 245 ms 37624 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 33 ms 8320 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 2 ms 256 KB Output is correct
42 Correct 2 ms 256 KB Output is correct