#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define solarmagic ios_base::sync_with_stdio(0),cin.tie(0)
struct P { ll X, Y; int idx;};
struct CHT{
vector<P> line;
int now;
CHT() { now = 0; }
ld f(ll i, ld x) { return line[i].X*x + line[i].Y; }
inline ld cross(int i, int j) {
return (ld)(line[i].Y - line[j].Y) / (ld)(line[j].X - line[i].X);
}
void insert(P a) {
line.push_back(a);
int i = SZ(line) - 1;
// cht코드에 문제가 있다면 이 if문만 보면 될 듯(나머진 맞을거임)
// (평행할 때 처리인데 이건 질문 게시판 나와 있는것처럼 평행하면 Y절편 이 높은걸 남김)
if (i > 1) {
if (line[i].X == line[i-1].X) {
if (line[i].Y > line[i-1].Y) line[i-1] = line[i];
line.pop_back();
return ;
}
}
while (i > 1 && cross(i, i-1) < cross(i-1, i-2)) {
line[i-1] = line.back();
line.pop_back();
i--;
}
}
pair<ll,int> get(ld k) {
do {
if (now == SZ(line)-1) return {f(now, k),line[now].idx};
ll cr = cross(now, now+1);
if (k < cr) return {f(now, k),line[now].idx};
now++;
} while (true);
}
void clear() {
line.clear();
now = 0;
}
} cht;
ll s[100001];
ll d[100001][2];
vector<int> zeros;
int track[100001][201];
int main() {
solarmagic;
ll n, k; cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> s[i];
if (s[i] == 0 && SZ(zeros) < k) zeros.push_back(i);
s[i] += s[i-1];
}
for (int j = 1; j <= k; j++) {
cht.clear();
cht.insert({0,0,-1});
for (int i = 1; i <= n; i++) {
auto p = cht.get(s[i]);
d[i][j%2] = p.first;
track[i][j] = p.second;
cht.insert({s[i],-s[i]*s[i]+d[i][1-j%2], i});
//cout << track[i][j] << ' ';
}
//cout << '\n';
}
cout << d[n][k%2] << '\n'; //아마 여기까진 맞을듯
// 트래킹할 때 0이 여러개 있으면 얘가 트래킹을 제대로 못해서 (ex: 6 4\n 2 1 0 0 0 0)
// 일단 구한다음에 unique한것만 남기고
// index가 zero인 얘들을 추가해주는 방식으로 함 (되나?)
vector<int> ans;
for (int i = k - 1; i >= 0; i--) {
n = track[n][i+1];
if (n == -1) break;
ans.push_back(n);
}
sort(ALL(ans));
ans.erase(unique(ALL(ans)), ans.end());
for (int i = SZ(ans), j = 0; i < k; i++, j++) {
ans.push_back(zeros[j]);
}
sort(ALL(ans));
for (auto i : ans) cout << i << ' ';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
2 ms |
380 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
2 ms |
380 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Incorrect |
2 ms |
376 KB |
contestant didn't find the optimal answer: 3698080247 < 3698080248 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
2 ms |
424 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
3 ms |
376 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Incorrect |
2 ms |
348 KB |
contestant didn't find the optimal answer: 933701 < 933702 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
508 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
4 ms |
504 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
2 ms |
476 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Incorrect |
3 ms |
508 KB |
contestant didn't find the optimal answer: 1019625816 < 1019625819 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1272 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
3 ms |
1144 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
12 ms |
1272 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
3 ms |
1272 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Incorrect |
14 ms |
1144 KB |
contestant didn't find the optimal answer: 679388318 < 679388326 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8888 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
10 ms |
8888 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Incorrect |
143 ms |
8820 KB |
contestant didn't find the optimal answer: 4973126687469560 < 4973126687469639 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
83556 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
106 ms |
83652 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Execution timed out |
2052 ms |
84320 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |