# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
110862 | evpipis | Split the sequence (APIO14_sequence) | C++17 | 2055 ms | 85388 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
const ll inf = 1e18;
const int len = 1e5+5;
int arr[len], pos, pref[len], po[len][205];
ll dp[len];
vector<pair<pll, int> > vec;
ll func(pll line, ll x){
return line.fi*x+line.se;
}
bool can(pll a, pll b, pll c){
if (b.fi == c.fi)
return (b.se <= c.se);
return ((double)(a.se-b.se)/(double)(b.fi-a.fi) >= (double)(b.se-c.se)/(double)(c.fi-b.fi));
}
void add(pair<pll, int> cur){
while (vec.size() >= 2 && can(vec[vec.size()-2].fi, vec.back().fi, cur.fi))
vec.pop_back();
vec.pb(cur);
}
pair<ll, int> query(ll x){
if (pos >= vec.size())
pos = vec.size()-1;
while (pos+1 < vec.size() && func(vec[pos+1].fi, x) > func(vec[pos].fi, x))
pos++;
return mp(func(vec[pos].fi, x), vec[pos].se);
}
int main(){
int n, k;
scanf("%d %d", &n, &k), k++;
for (int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
for (int i = 1; i <= n; i++)
pref[i] = pref[i-1]+arr[i];
for (int i = 1; i <= n; i++)
dp[i] = -inf;
for (int j = 1; j <= k; j++){
vec.clear();
pos = 0;
add(mp(mp(pref[j-1], dp[j-1]-pref[j-1]*1LL*pref[j-1]), j-1));
for (int i = j; i <= n; i++){
pair<ll, int> ans = query(pref[i]);
po[i][j] = ans.se;
add(mp(mp(pref[i], dp[i]-pref[i]*1LL*pref[i]), i));
dp[i] = ans.fi;
}
}
printf("%lld\n", dp[n]);
int cur = n;
for (int j = k; j > 1; j--){
cur = po[cur][j];
printf("%d ", cur);
}
printf("\n");
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
컴파일 시 표준 에러 (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... |