#include<bits/stdc++.h>
using namespace std;
#define f(a, b, c) for(int a = (b); a <= (c); ++a)
#define fr(a, b, c) for(int a = (b); a >= (c); --a)
#define ff first
#define ss second
#define ld long double
#define ll long long
#define vt vector
#define pb push_back
#define bit(x, i) (((x) >> (i)) & 1)
#define reset(x, v) memset(x, v, sizeof(x));
#define all(x) (x).begin(), (x).end()
#define Time cerr<<"[It took "<<fixed<<setprecision(3)<<double(clock()-time)/CLOCKS_PER_SEC<<"s]"<<"\n"
#define dbg(...) cout<<"LINE("<<__LINE__<<") -> ["<<#__VA_ARGS__<<"]: [",DBG(__VA_ARGS__)
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
void DBG() { cout << "]" << "\n"; }
string to_string(string strin) { return strin; }
string to_string(vt<int> &vec) {
string t = "";
f(i, 0, vec.size() - 1) t += to_string(vec[i]) + (i ^ vec.size() - 1 ? " " : "");
return t;
}
template<class H, class... T> void DBG(H h, T... t) {
cout << to_string(h); if(sizeof...(t)) cout << ", "; DBG(t...);
}
// init done
#define NAME "a"
#define MOD (ll)1000000007
#define ii pair<int, int>
#define oo (ll)1e18
#define N (ll)1e5 + 5
int n, k;
int a[N];
int L[N];
vt<ll> T, line;
void update(int id, int l, int r, int p)
{
if(p > r || p < l)
return;
if(l == r)
T[id] = line[p];
else {
int mid = (l + r) >> 1;
update(id << 1, l, mid, p);
update(id << 1 | 1, mid + 1, r, p);
T[id] = min(T[id << 1], T[id << 1 | 1]);
}
}
ll getmin(int id, int l, int r, int le, int ri)
{
if(le > r || ri < l)
return oo;
if(l >= le && r <= ri)
return T[id];
int mid = (l + r) >> 1;
return min(getmin(id << 1, l, mid, le, ri),
getmin(id << 1 | 1, mid + 1, r, le, ri));
}
void solve()
{
cin >> n >> k;
f(i, 1, n) {
cin >> a[i];
}
line.resize(n + 2, 0);
T.assign(500000, 0);
vt<vt<ll>> dp(k + 2, vt<ll>(n + 2, oo));
f(i, 1, n) {
L[i] = i - 1;
while(L[i] && a[L[i]] <= a[i])
L[i] = L[L[i]];
}
// f(i, 1, n) cout << L[i] << " "; cout << "\n";
f(i, 1, n) {
dp[1][i] = *max_element(a + 1, a + i + 1);
}
f(i, 2, k) {
f(j, 1, n) {
line[j] = dp[i - 1][j];
update(1, 1, n, j);
}
f(j, i, n) {
int tmp = j;
do {
dp[i][j] = min(dp[i][j],
getmin(1, 1, n, (L[tmp] > 0 ? L[tmp] : 1), tmp - 1) + (ll)a[tmp]);
tmp = L[tmp];
}
while(tmp > 1);
}
}
cout << dp[k][n] << "\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
auto time = clock();
// freopen(NAME".inp", "r", stdin);
// freopen(NAME".out", "w", stdout);
int T = 1;
// cin >> T;
while(T--) {
solve();
}
// Time;
return 0;
}
Compilation message
blocks.cpp: In function 'std::string to_string(std::vector<int>&)':
blocks.cpp:4:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define f(a, b, c) for(int a = (b); a <= (c); ++a)
| ^
blocks.cpp:26:5: note: in expansion of macro 'f'
26 | f(i, 0, vec.size() - 1) t += to_string(vec[i]) + (i ^ vec.size() - 1 ? " " : "");
| ^
blocks.cpp:26:70: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
26 | f(i, 0, vec.size() - 1) t += to_string(vec[i]) + (i ^ vec.size() - 1 ? " " : "");
| ~~~~~~~~~~~^~~
blocks.cpp: In function 'int main()':
blocks.cpp:117:10: warning: unused variable 'time' [-Wunused-variable]
117 | auto time = clock();
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4224 KB |
Output is correct |
2 |
Correct |
3 ms |
4224 KB |
Output is correct |
3 |
Correct |
3 ms |
4224 KB |
Output is correct |
4 |
Correct |
3 ms |
4224 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
3 ms |
4224 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
3 ms |
4224 KB |
Output is correct |
14 |
Correct |
3 ms |
4224 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
3 ms |
4224 KB |
Output is correct |
17 |
Correct |
3 ms |
4224 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4224 KB |
Output is correct |
2 |
Correct |
3 ms |
4224 KB |
Output is correct |
3 |
Correct |
4 ms |
4224 KB |
Output is correct |
4 |
Correct |
4 ms |
4224 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
4 ms |
4224 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
3 ms |
4224 KB |
Output is correct |
14 |
Correct |
3 ms |
4224 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
3 ms |
4224 KB |
Output is correct |
17 |
Correct |
3 ms |
4224 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
4 ms |
4224 KB |
Output is correct |
20 |
Correct |
3 ms |
4224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4352 KB |
Output is correct |
2 |
Correct |
3 ms |
4208 KB |
Output is correct |
3 |
Correct |
3 ms |
4256 KB |
Output is correct |
4 |
Correct |
3 ms |
4256 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
3 ms |
4352 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
3 ms |
4352 KB |
Output is correct |
14 |
Correct |
4 ms |
4352 KB |
Output is correct |
15 |
Correct |
5 ms |
4352 KB |
Output is correct |
16 |
Correct |
6 ms |
4352 KB |
Output is correct |
17 |
Correct |
4 ms |
4352 KB |
Output is correct |
18 |
Correct |
4 ms |
4352 KB |
Output is correct |
19 |
Correct |
12 ms |
4360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
6776 KB |
Output is correct |
2 |
Execution timed out |
1079 ms |
6528 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |