#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, k;
int a[N];
ll dp[N][105];
int f[17][N], lg[N];
void build()
{
for(int i = 1; i <= n; i++)
f[0][i] = a[i];
for(int k = 1; k <= lg[n]; k++)
for(int i = 1; i <= n; i++)
{
int j = i + (1 << (k - 1));
if(j > n)
f[k][i] = f[k - 1][i];
else
f[k][i] = max(f[k - 1][i], f[k - 1][j]);
}
}
int get(int l, int r)
{
int j = r - l + 1, k = lg[j];
return max(f[k][l], f[k][r - (1 << k) + 1]);
}
void solve()
{
cin >> n >> k;
for(int i = 2; i <= n; i++)
lg[i] = lg[i / 2] + 1; // log2
for(int i = 1; i <= n; i++)
cin >> a[i];
build();
for(int i = 0; i <= n; i++)
for(int j = 0; j <= k; j++)
dp[i][j] = 1e17;
auto get_sum = [&](int i, int j, int pos)
{
return dp[i][j] + get(i + 1, pos);
};
dp[0][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= k; j++)
{
int l = 0, r = i - 1;
while(r - l > 3)
{
int midl = l + (r - l) / 3;
int midr = r - (r - l) / 3;
if(get_sum(midl, j - 1, i) > get_sum(midr, j - 1, i))
l = midl;
else
r = midr;
}
for(int k = l; k <= r; k++)
dp[i][j] = min(dp[i][j], dp[k][j - 1] + get(k + 1, i));
}
}
cout << dp[n][k];
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |