#include <bits/stdc++.h>
#include <random>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define o cout<<"BUG"<<endl;
#define IOS ios_base::sync_with_stdio(0);
#define en "\n"
#define FOR(i, j, n) for(int j = i; j < n; ++j)
#define forn(i, j, n) for(int j = i; j <= n; ++j)
#define nfor(i, j, n) for(int j = n; j >= i; --j)
#define sortv(vv) sort(vv.begin(), vv.end())
#define all(v) v.begin(), v.end()
#define ld long double
#define ull unsigned long long
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
const ll maxn=1e5+100, inf=1e18,LOG=18,mod=1e9+7;
ll block = 300, timer = 0;
const ld EPS = 1e-7;
#define bt(i) (1 << (i))
#define int ll
#define y1 yy
#define pii pair <int, int>
int n, k, a[maxn], step[maxn], opt[maxn][110], dp[maxn][110];
int parse[maxn][LOG];
int calc(int l, int r)
{
int len = r - l + 1;
//7 8 9 10 11
return max(parse[l][step[len]], parse[r - (1 << step[len]) + 1][step[len]]);
}
main()
{
IOS
cin >> n >> k;
step[1] = 0;
forn(2, i, n)
{
step[i] = step[i - 1];
if((1 << (step[i] + 1)) <= i)
{
step[i]++;
}
}
forn(1, i, n)
{
cin >> a[i];
parse[i][0] = a[i];
}
forn(1, j, LOG - 1)
{
forn(1, i, n)
{
if(i + (1 << (j - 1)) <= n)
parse[i][j] = max(parse[i][j - 1], parse[i + (1 << (j - 1))][j - 1]);
else
parse[i][j] = parse[i][j - 1];
}
}
forn(1, j, k)
{
opt[n + 1][j] = n;
}
forn(0, j, k + 1)
{
forn(0, i, n + 1)
{
dp[i][j] = inf;
opt[i][j] = 1;
}
}
dp[0][0] = 0;
forn(1, i, n)
{
dp[i][1] = calc(1, i);
opt[i][1] = 1;
}
forn(2, j, k)
{
nfor(j, i, n)
{
dp[i][j] = inf;
//dp[i][j]
//opt[i][j - 1] <= opt[i][j] <= opt[i + 1][j]
forn(opt[i][j - 1], dot, opt[i + 1][j])
{
if(dp[dot - 1][j - 1] + calc(dot, i) < dp[i][j])
{
dp[i][j] = dp[dot - 1][j - 1] + calc(dot, i);
opt[i][j] = dot;
}
}
// cout << i << " " << j << " " << dp[i][j] << " " << opt[i][j] << " " << endl;
}
}
cout << dp[n][k];
}
Compilation message
blocks.cpp:45:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
19200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |