This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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=1e9,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 - 1;
}
//dp[i][j]
//opt[i][j - 1] <= opt[i][j] <= opt[i + 1][j]
forn(0, j, k + 1)
{
forn(0, i, n + 1)
{
dp[i][j] = inf;
}
}
dp[0][0] = 0;
forn(1, i, n)
{
dp[i][1] = calc(1, i);
}
forn(2, j, k)
{
nfor(j, i, n)
{
dp[i][j] = inf;
forn(opt[i][j - 1], dot, opt[i + 1][j])
{
if(dp[dot][j - 1] + calc(dot + 1, i) < dp[i][j])
{
dp[i][j] = dp[dot][j - 1] + calc(dot + 1, i);
opt[i][j] = dot;
}
}
// cout << i << " " << j << " " << dp[i][j] << " " << opt[i][j] << " " << endl;
}
}
cout << dp[n][k];
}
Compilation message (stderr)
blocks.cpp:45:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |