답안 #91380

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91380 2018-12-27T09:23:08 Z Just_Solve_The_Problem K개의 묶음 (IZhO14_blocks) C++11
100 / 100
168 ms 13668 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int)1e5 + 7;
const int inf = (int)1e9 + 7;

int n, k;
int dp[2][N];
int a[N];
main() {
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	stack < pair < int, int > > st;
	for (int i = 1; i <= n; i++) {
		dp[1][i] = max(dp[1][i - 1], a[i]);
	}
	for (int i = 2; i <= k; i++) {
		while (!st.empty()) st.pop();
		st.push({dp[i & 1 ^ 1][i - 1], a[i]});
		dp[i & 1][i] = dp[i & 1 ^ 1][i - 1] + a[i];
		for (int j = i + 1; j <= n; j++) {
			int c = dp[i & 1 ^ 1][j - 1];
			while (!st.empty() && st.top().second <= a[j]) {
				c = min(c, st.top().first);
				st.pop();
			}
			if (st.empty() || st.top().first + st.top().second > c + a[j]) {
				st.push({c, a[j]});
			}
			dp[i & 1][j] = st.top().first + st.top().second;
		}
	}
	cout << dp[k & 1][n];
}

Compilation message

blocks.cpp:11:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
blocks.cpp: In function 'int main()':
blocks.cpp:22:17: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   st.push({dp[i & 1 ^ 1][i - 1], a[i]});
               ~~^~~
blocks.cpp:23:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   dp[i & 1][i] = dp[i & 1 ^ 1][i - 1] + a[i];
                     ~~^~~
blocks.cpp:25:17: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
    int c = dp[i & 1 ^ 1][j - 1];
               ~~^~~
blocks.cpp:12: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:14:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 472 KB Output is correct
3 Correct 2 ms 472 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 628 KB Output is correct
6 Correct 2 ms 628 KB Output is correct
7 Correct 2 ms 628 KB Output is correct
8 Correct 2 ms 628 KB Output is correct
9 Correct 2 ms 628 KB Output is correct
10 Correct 2 ms 628 KB Output is correct
11 Correct 2 ms 628 KB Output is correct
12 Correct 2 ms 628 KB Output is correct
13 Correct 2 ms 628 KB Output is correct
14 Correct 2 ms 628 KB Output is correct
15 Correct 2 ms 660 KB Output is correct
16 Correct 2 ms 660 KB Output is correct
17 Correct 2 ms 660 KB Output is correct
18 Correct 2 ms 660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 660 KB Output is correct
2 Correct 1 ms 664 KB Output is correct
3 Correct 2 ms 664 KB Output is correct
4 Correct 2 ms 664 KB Output is correct
5 Correct 2 ms 664 KB Output is correct
6 Correct 2 ms 664 KB Output is correct
7 Correct 2 ms 664 KB Output is correct
8 Correct 2 ms 664 KB Output is correct
9 Correct 2 ms 664 KB Output is correct
10 Correct 2 ms 664 KB Output is correct
11 Correct 2 ms 664 KB Output is correct
12 Correct 2 ms 664 KB Output is correct
13 Correct 2 ms 668 KB Output is correct
14 Correct 2 ms 672 KB Output is correct
15 Correct 2 ms 672 KB Output is correct
16 Correct 2 ms 672 KB Output is correct
17 Correct 2 ms 672 KB Output is correct
18 Correct 2 ms 672 KB Output is correct
19 Correct 2 ms 672 KB Output is correct
20 Correct 2 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 696 KB Output is correct
2 Correct 2 ms 696 KB Output is correct
3 Correct 2 ms 700 KB Output is correct
4 Correct 2 ms 704 KB Output is correct
5 Correct 2 ms 708 KB Output is correct
6 Correct 2 ms 712 KB Output is correct
7 Correct 2 ms 716 KB Output is correct
8 Correct 2 ms 720 KB Output is correct
9 Correct 2 ms 720 KB Output is correct
10 Correct 2 ms 728 KB Output is correct
11 Correct 2 ms 732 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 848 KB Output is correct
16 Correct 2 ms 848 KB Output is correct
17 Correct 2 ms 848 KB Output is correct
18 Correct 2 ms 848 KB Output is correct
19 Correct 2 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 848 KB Output is correct
2 Correct 7 ms 1396 KB Output is correct
3 Correct 9 ms 1828 KB Output is correct
4 Correct 38 ms 2132 KB Output is correct
5 Correct 104 ms 3440 KB Output is correct
6 Correct 15 ms 4120 KB Output is correct
7 Correct 55 ms 4928 KB Output is correct
8 Correct 2 ms 4928 KB Output is correct
9 Correct 2 ms 4928 KB Output is correct
10 Correct 2 ms 4928 KB Output is correct
11 Correct 2 ms 4928 KB Output is correct
12 Correct 2 ms 4928 KB Output is correct
13 Correct 2 ms 4928 KB Output is correct
14 Correct 11 ms 4928 KB Output is correct
15 Correct 3 ms 4928 KB Output is correct
16 Correct 5 ms 4928 KB Output is correct
17 Correct 7 ms 4928 KB Output is correct
18 Correct 9 ms 5116 KB Output is correct
19 Correct 33 ms 5448 KB Output is correct
20 Correct 80 ms 6624 KB Output is correct
21 Correct 16 ms 7300 KB Output is correct
22 Correct 45 ms 7976 KB Output is correct
23 Correct 14 ms 8220 KB Output is correct
24 Correct 20 ms 9364 KB Output is correct
25 Correct 85 ms 9988 KB Output is correct
26 Correct 2 ms 9988 KB Output is correct
27 Correct 2 ms 9988 KB Output is correct
28 Correct 18 ms 9988 KB Output is correct
29 Correct 3 ms 9988 KB Output is correct
30 Correct 7 ms 9988 KB Output is correct
31 Correct 7 ms 9988 KB Output is correct
32 Correct 11 ms 10368 KB Output is correct
33 Correct 59 ms 10612 KB Output is correct
34 Correct 168 ms 12232 KB Output is correct
35 Correct 17 ms 12824 KB Output is correct
36 Correct 81 ms 13668 KB Output is correct
37 Correct 3 ms 13668 KB Output is correct
38 Correct 12 ms 13668 KB Output is correct
39 Correct 2 ms 13668 KB Output is correct
40 Correct 2 ms 13668 KB Output is correct
41 Correct 2 ms 13668 KB Output is correct
42 Correct 2 ms 13668 KB Output is correct