# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
918483 |
2024-01-29T22:32:19 Z |
n3rm1n |
K blocks (IZhO14_blocks) |
C++17 |
|
1 ms |
2396 KB |
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const long long MAXN = 1e5 + 10, MAXK = 105;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long n, k, a[MAXN];
void read()
{
cin >> n >> k;
for (long long i = 1; i <= n; ++ i)
cin >> a[i];
}
long long dp[MAXN][MAXK];
void solve()
{
long long inf = 1e17;
for (long long j = 1; j <= k; ++ j)
{
stack < pair <long long, long long > > t;
for (long long i = j; i <= n; ++ i)
{
dp[i][j] = inf;
dp[i][j] = dp[i-1][j-1] + a[i];
long long init = dp[i][j];
while(!t.empty())
{
long long sz = t.top().first;
long long dpp = t.top().second;
if(sz <= a[i])
{
if(dpp + a[i] - sz < dp[i][j])dp[i][j] = dpp + a[i] - sz;
t.pop();
}
else break;
}
if(t.size() == 0 || t.top().second > dp[i][j])t.push(make_pair(a[i], dp[i][j]));
else dp[i][j] = min(dp[i][j], t.top().second);
//cout << i << " " << j << " --> " << dp[i][j] << endl;
}
}
cout << dp[n][k] << endl;
}
int main()
{
speed();
read();
solve();
return 0;
}
Compilation message
blocks.cpp: In function 'void solve()':
blocks.cpp:29:23: warning: unused variable 'init' [-Wunused-variable]
29 | long long init = dp[i][j];
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |