Submission #155973

#TimeUsernameProblemLanguageResultExecution timeMemory
155973solarmagicSplit the sequence (APIO14_sequence)C++17
100 / 100
1010 ms83776 KiB
#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; } ll 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(ll k) { do { if (now == SZ(line)-1) return {f(now, k),line[now].idx}; ld cr = cross(now, now+1); if (abs(cr-k) < 0.0000000001) {now++; continue;} // 실수 오차가 있음 ㅡ.ㅡ; 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[201][100001]; 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[j][i] = p.second; cht.insert({s[i],-s[i]*s[i]+d[i][1-j%2], i}); } } cout << d[n][k%2] << '\n'; vector<int> ans; for (int i = k - 1; i >= 0; i--) { n = track[i+1][n]; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...