답안 #343651

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343651 2021-01-04T10:34:59 Z emil_physmath K개의 묶음 (IZhO14_blocks) C++17
53 / 100
1000 ms 136556 KB
#define BUGO(x) cerr << #x << " = " << (x) << '\n';
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
using ll = long long;
ll inf = 1'000'000'000'000'000'000LL;
ll dp[100'002][102], a[100'002];
ll mn[4 * 100'002][102];
struct el {
    ll x; int ind;
};
bool operator<(const el& a, const el& b) {
    return a.x < b.x;
}
void Build(int v, int vl, int vr, int k) {
    if(vl == vr) {
        mn[v][k] = dp[vr][k];
        return;
    }
    int m = (vl + vr) / 2;
    Build(v * 2, vl, m, k);
    Build(v * 2 + 1, m + 1, vr, k);
    mn[v][k] = min(mn[v * 2][k], mn[v * 2 + 1][k]);
}
ll Mn(int v, int vl, int vr, int k, int l, int r){
    if(vl == l && vr == r)
        return mn[v][k];
    int m = (vl + vr) / 2;
    ll res = inf;
    if(l <= m) res = min(res, Mn(v * 2, vl, m, k, l, min(r, m)));
    if(r > m) res = min(res, Mn(v * 2 + 1, m + 1, vr, k, max(l, m + 1), r));
    return res;
}
int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
    for(int i = 0; i <= n; i++)
        for(int curk = 1; curk <= k; curk++)
            dp[i][curk] = inf;

    for(int curk = 1; curk <= k; curk++) {
        stack<el> x;
        x.push({inf, 0});
        for(int i = 1; i <= n; i++) {
            while(x.top().x < a[i])x.pop();
            el prev = x.top();
            x.push({a[i], i});
            if(i < curk)
                continue;
            if (curk == 1)
                dp[i][curk] = i > 1 ? max(dp[i - 1][1], a[i]) : a[i];
            else if (prev.ind == 0)
                dp[i][curk] = ((i - 1 == 0) ? 0: Mn(1, 1, n, curk - 1, 1, i - 1)) + a[i];
            else
                dp[i][curk] = min(dp[prev.ind][curk], Mn(1, 1, n, curk - 1, prev.ind, i - 1) + a[i]);
        }
        Build(1, 1, n, curk);
    }

	/*for (int i = 0; i <= n; i++)
	{
		for (int curk = 0; curk <= k; curk++)
			cerr << dp[i][curk] << ' ';
		cerr << '\n';
	}*/
	cout << dp[n][k] << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 380 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 380 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 620 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 640 KB Output is correct
52 Correct 1 ms 620 KB Output is correct
53 Correct 1 ms 620 KB Output is correct
54 Correct 1 ms 620 KB Output is correct
55 Correct 1 ms 620 KB Output is correct
56 Correct 1 ms 620 KB Output is correct
57 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 380 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 620 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 640 KB Output is correct
52 Correct 1 ms 620 KB Output is correct
53 Correct 1 ms 620 KB Output is correct
54 Correct 1 ms 620 KB Output is correct
55 Correct 1 ms 620 KB Output is correct
56 Correct 1 ms 620 KB Output is correct
57 Correct 2 ms 620 KB Output is correct
58 Correct 75 ms 30316 KB Output is correct
59 Correct 80 ms 133356 KB Output is correct
60 Correct 185 ms 133356 KB Output is correct
61 Execution timed out 1107 ms 136556 KB Time limit exceeded
62 Halted 0 ms 0 KB -