이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n, k, where[201][100001];
ll pref[100001]{0}, dp[2][100001], q[100001], l = 1, r = 1;
vi arr;
bool test1(int x, int y, int i){
return (dp[0][y] - dp[0][x] >= (pref[y]-pref[x]) * (pref[n]-pref[i]));
}
bool test2(int x, int y, int i){
return ((dp[0][y] - dp[0][x]) * (pref[i] - pref[y]) <= (dp[0][i]-dp[0][y])*(pref[y]-pref[x]));
}
int main(){
cin>>n>>k;
arr.resize(n+1);
for(int i = 1; i <= n; i++){
cin>>arr[i];
pref[i] = pref[i-1]+arr[i];
}
fill(dp[0], dp[0]+n+1, 0);
for(int i = 1; i <= k; i++){
q[r++] = 0;
for(int j = 1; j <= n; j++){
while(r-l > 1 && test1(q[l], q[l+1], j)) l++;
ll x = q[l];
dp[1][j] = dp[0][x] + (pref[j] - pref[x]) * (pref[n] - pref[j]);
where[i][j] = x;
while(r-l>1 && test2(q[r-2], q[r-1], j)) r--;
q[r++] = j;
}
l = r = 1;
for(int j = 1; j <= n; j++) dp[0][j] = dp[1][j];
}
ll ans =-1;
int idx = -1;
for(int i = 1; i <= n; i++){
if(dp[0][i] > ans){
ans = dp[0][i];
idx = i;
}
}
cout<<ans<<endl;
for(int i = 0; i < k; i++){
cout<<idx<<" ";
idx = where[k-1][idx];
}
cout<<endl;
return 0;
}
# | 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... |