Submission #155968

#TimeUsernameProblemLanguageResultExecution timeMemory
155968solarmagicSplit the sequence (APIO14_sequence)C++17
0 / 100
2065 ms83856 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}; 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 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...