# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1106715 | luvna | 수열 (APIO14_sequence) | C++14 | 136 ms | 131072 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define int long long
using namespace std;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int N = 1e5 + 15;
int n, k;
int pref[N];
ll dp[2][N];
bool past = false, succ = true;
struct line {
ll m, c, id;
ll compute(ll x) {return m * x + c;}
};
bool useless(line d1, line d2, line d3) {
return (d1.c - d2.c) * (d3.m - d2.m) >= (d2.c - d3.c) * (d2.m - d1.m);
}
struct CHT {
vector<line> lines;
void reload() {
while(!lines.empty()) lines.pop_back(); lines.push_back({0, 0, 0});
}
void add(line d) {
while(lines.size() > 1 && useless(d, lines.back(), lines[lines.size() - 2]))
lines.pop_back();
lines.push_back(d);
}
pii query(ll x) {
int l = 0, r = lines.size() - 1;
while(l < r) {
int m = (l + r) >> 1;
if(lines[m].compute(x) <= lines[m + 1].compute(x)) l = m + 1;
else r = m;
}
return {lines[l].compute(x), lines[l].id};
}
};
CHT solver;
int trace[N][205];
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> pref[i], pref[i] += pref[i-1];
for(int j = 1; j <= k; j++){
solver.reload(); swap(succ, past);
for(int i = 1; i <= n; i++){
pii e = solver.query(pref[n] - pref[i]);
trace[i][j] = e.se;
dp[succ][i] = e.fi + 1LL*pref[i]*(pref[n] - pref[i]);
solver.add({-pref[i], dp[past][i], i});
}
}
int pos = -1;
ll ans = -1;
for(int i = 1; i <= n; i++){
if(maximize(ans, dp[succ][i])) pos = i;
}
vector<int> res;
for(int i = k; i >= 1; i--) res.push_back(pos), pos = trace[pos][i];
reverse(all(res));
cout << ans << endl;
for(int x : res) cout << x << " ";
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |