#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
//#define endl '\n'
#define ll long long
#define pll pair<ll,ll>
#define pi pair<int,int>
using namespace std;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAXN = 1e9+1;
const int N = 1005;
struct Line{
ll m, b, id;
ll eval(ll x) {
return m*x + b;
}
};
struct node{
node *l, *r;
Line ln;
node(node* _l, node* _r, Line _ln):
l(_l), r(_r), ln(_ln) {};
};
typedef node* pnode;
Line get(pnode rt) {
return (!rt ? Line({0, -INF, -1}) : rt -> ln);
}
void add(pnode &rt, Line ln, int l=0, int r=MAXN, int v = 1) {
if (!rt) rt = new node(NULL, NULL, Line({0, -INF, -1}));
int m = (l + r) >> 1;
bool left = ln.eval(l) > get(rt).eval(l);
bool mid = ln.eval(m) > get(rt).eval(m);
if (mid) swap(ln, rt -> ln);
if (r - l == 1) return;
if (left != mid) add(rt -> l, ln, l, m, v << 1);
else add(rt -> r, ln, m, r, v << 1 | 1);
}
pll get(pnode &rt, ll x, int l=0, int r=MAXN, int v=1) {
if (rt == NULL) rt = new node(NULL, NULL, Line({0, -INF, -1}));
int m = (l + r) >> 1;
pll now = {get(rt).eval(x), get(rt).id};
if (r - l == 1) return now;
if (x < m) return max(now, get(rt -> l, x, l, m, v << 1));
else return max(now, get(rt -> r, x, m, r, v << 1 | 1));
}
ll dp[201][N];
int opt[201][N], ps[N];
pnode rt;
int main() {
ios_base :: sync_with_stdio(0);
cin.tie(0);
int n, K;
cin >> n >> K; K++;
for (int i = 1, x; i <= n; i++) {
cin >> x;
ps[i] = ps[i - 1] + x;
dp[0][i] = -INF;
}
dp[0][0] = 0;
pll ans;
for (int k = 1; k <= K; k++) {
rt = NULL;
if (k == 1) add(rt, Line({0, 0, 0}));
for (int i = 1; i <= n; i++) {
//cout << "getting ans for " << ps[i] << endl;
ans = get(rt, ps[i]);
dp[k][i] = ans.F;
opt[k][i] = ans.S;
//cout << k << " " << i << " " << opt[k][i] << endl;
add(rt, Line({ps[i], dp[k - 1][i] - 1ll*ps[i]*ps[i], i}));
}
}
cout << dp[K][n] << endl;
vector<int> res;
int id = n, ptr = K;
while(res.size() < K - 1) {
res.pb(opt[ptr][id]);
id = opt[ptr--][id];
}
reverse(res.begin(), res.end());
for (int i : res) cout << i << " ";
return 0;
}
Compilation message
sequence.cpp: In function 'int main()':
sequence.cpp:85:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
85 | while(res.size() < K - 1) {
| ~~~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
1 ms |
640 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
1 ms |
396 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
5 ms |
3584 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
640 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
768 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
768 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
3 ms |
1920 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
896 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
768 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
2 ms |
1152 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
640 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
65 ms |
43512 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
2 ms |
1152 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
21 ms |
8448 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
26 ms |
10616 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
64 ms |
40696 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
11 ms |
6016 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
8 ms |
4768 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
15 ms |
8960 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
3 ms |
1152 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Runtime error |
213 ms |
131076 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
2304 KB |
declared answer doesn't correspond to the split scheme: declared = 11612281, real = 1033897070 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
141 ms |
3540 KB |
declared answer doesn't correspond to the split scheme: declared = 4367201737, real = 7931375185 |
2 |
Halted |
0 ms |
0 KB |
- |