# include <bits/stdc++.h>
# define pb push_back
# define fr first
# define sc second
# define mk make_pair
using namespace std;
const int inf = 1e9 + 7;
const int N = 1e5 + 5;
typedef long long ll;
int n, a[N], k, t[N * 4];
ll dp[101][N];
void build(int v = 1, int tl = 1, int tr = n)
{
if(tl == tr)
t[v] = a[tl];
else
{
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm);
build((v << 1) | 1, tm + 1, tr);
t[v] = max(t[v << 1], t[(v << 1) | 1]);
}
}
int get(int l, int r, int v = 1, int tl = 1, int tr = n)
{
if(l > tr || tl > r) return 0;
if(l <= tl && tr <= r) return t[v];
int tm = (tl + tr) >> 1;
return max(get(l, r, v << 1, tl, tm),
get(l, r, (v << 1) | 1, tm + 1, tr));
}
void dv(int k, int l, int r, int opl, int opr)
{
if(l > r) return ;
int md = (l + r) >> 1, opt;
dp[k][md] = 1e18;
for(int i = opl; i <= min(md, opr); i ++)
{
ll now = dp[k - 1][i - 1] + get(i, md);
if(now <= dp[k][md])
{
dp[k][md] = now;
opt = i;
}
}
dv(k, l, md - 1, opl, opt);
dv(k, md + 1, r, opt, opr);
}
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
int mx = 0;
build();
memset(dp, 127, sizeof(dp));
for(int i = 1; i <= n; i ++)
{
mx = max(mx, a[i]);
dp[1][i] = mx;
}
for(int i = 2; i <= k; i ++)
{
for(int j = i; j <= n; j ++)
{
for(int f = j; f <= n; f ++)
{
dp[i][f] = min(dp[i][f], dp[i - 1][j - 1] + get(j, f));
}
}
}
cout << dp[k][n] << endl;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:67:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
^
blocks.cpp: In function 'void dv(int, int, int, int, int)':
blocks.cpp:58:33: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
dv(k, l, md - 1, opl, opt);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
82880 KB |
Output is correct |
2 |
Correct |
3 ms |
82880 KB |
Output is correct |
3 |
Correct |
6 ms |
82880 KB |
Output is correct |
4 |
Correct |
3 ms |
82880 KB |
Output is correct |
5 |
Correct |
13 ms |
82880 KB |
Output is correct |
6 |
Correct |
9 ms |
82880 KB |
Output is correct |
7 |
Correct |
13 ms |
82880 KB |
Output is correct |
8 |
Correct |
3 ms |
82880 KB |
Output is correct |
9 |
Correct |
6 ms |
82880 KB |
Output is correct |
10 |
Correct |
3 ms |
82880 KB |
Output is correct |
11 |
Correct |
13 ms |
82880 KB |
Output is correct |
12 |
Correct |
0 ms |
82880 KB |
Output is correct |
13 |
Correct |
6 ms |
82880 KB |
Output is correct |
14 |
Correct |
6 ms |
82880 KB |
Output is correct |
15 |
Correct |
3 ms |
82880 KB |
Output is correct |
16 |
Correct |
6 ms |
82880 KB |
Output is correct |
17 |
Correct |
6 ms |
82880 KB |
Output is correct |
18 |
Correct |
3 ms |
82880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
82880 KB |
Output is correct |
2 |
Correct |
6 ms |
82880 KB |
Output is correct |
3 |
Correct |
9 ms |
82880 KB |
Output is correct |
4 |
Correct |
13 ms |
82880 KB |
Output is correct |
5 |
Correct |
3 ms |
82880 KB |
Output is correct |
6 |
Correct |
3 ms |
82880 KB |
Output is correct |
7 |
Correct |
13 ms |
82880 KB |
Output is correct |
8 |
Correct |
9 ms |
82880 KB |
Output is correct |
9 |
Correct |
0 ms |
82880 KB |
Output is correct |
10 |
Correct |
6 ms |
82880 KB |
Output is correct |
11 |
Correct |
6 ms |
82880 KB |
Output is correct |
12 |
Correct |
6 ms |
82880 KB |
Output is correct |
13 |
Correct |
9 ms |
82880 KB |
Output is correct |
14 |
Correct |
0 ms |
82880 KB |
Output is correct |
15 |
Correct |
3 ms |
82880 KB |
Output is correct |
16 |
Correct |
0 ms |
82880 KB |
Output is correct |
17 |
Correct |
6 ms |
82880 KB |
Output is correct |
18 |
Correct |
0 ms |
82880 KB |
Output is correct |
19 |
Correct |
9 ms |
82880 KB |
Output is correct |
20 |
Correct |
9 ms |
82880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
82880 KB |
Output is correct |
2 |
Correct |
6 ms |
82880 KB |
Output is correct |
3 |
Correct |
3 ms |
82880 KB |
Output is correct |
4 |
Correct |
9 ms |
82880 KB |
Output is correct |
5 |
Correct |
6 ms |
82880 KB |
Output is correct |
6 |
Correct |
3 ms |
82880 KB |
Output is correct |
7 |
Correct |
6 ms |
82880 KB |
Output is correct |
8 |
Correct |
9 ms |
82880 KB |
Output is correct |
9 |
Correct |
13 ms |
82880 KB |
Output is correct |
10 |
Correct |
3 ms |
82880 KB |
Output is correct |
11 |
Correct |
9 ms |
82880 KB |
Output is correct |
12 |
Correct |
3 ms |
82880 KB |
Output is correct |
13 |
Correct |
16 ms |
82880 KB |
Output is correct |
14 |
Correct |
23 ms |
82880 KB |
Output is correct |
15 |
Correct |
39 ms |
82880 KB |
Output is correct |
16 |
Correct |
19 ms |
82880 KB |
Output is correct |
17 |
Correct |
36 ms |
82880 KB |
Output is correct |
18 |
Correct |
23 ms |
82880 KB |
Output is correct |
19 |
Correct |
29 ms |
82880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1000 ms |
82880 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |