이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) a.begin(),a.end()
#define mp make_pair
#define fi first
#define se second
#define sz(a) (int)a.size()
const int maxn = (int)1e4+10;
const int INF = (int)2e18;
int n, K, a[maxn], pre[maxn];
int dp[maxn][201], p[maxn][201];
vector<int> v;
void recur(int i, int k){ if(k) v.pb(i), recur(p[i][k],k-1); }
double slope(int i, int j, int k){
//if(i>j)swap(i,j); if(i==0) return (double)dp[j][k]/pre[j];
return (double)(dp[i][k]-dp[j][k])/(pre[i]-pre[j]);
}
bool case1(int i, int j, int k, int ind){
return (dp[j][k]-dp[i][k])>=(pre[j]-pre[i])*(pre[n]-pre[ind]);
}
bool case2(int i, int j, int k, int ind){
return (dp[j][k]-dp[i][k])*(pre[ind]-pre[j])<=(dp[ind][k]-dp[j][k])*(pre[j]-pre[ind]);
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> K; int ans = -(int)1e18;
for(int i = 0; i < n; i++) cin >> a[i], pre[i+1]=pre[i]+a[i];
for(int k = 1; k <= K+1; k++){
deque<int> DQ; DQ.clear(); DQ.push_back(0);
for(int i = 1; i <= n; i++){
while(sz(DQ)>1 and case1(DQ[0],DQ[1],k-1,i)) DQ.pop_front();
int j = DQ[0]; int tot = dp[j][k-1]+(pre[i]-pre[j])*(pre[n]-pre[i]);
if(dp[i][k] < tot) dp[i][k]=tot, p[i][k]=j;
while(sz(DQ)>1 and case2(DQ[sz(DQ)-2],DQ.back(),k-1,i)) DQ.pop_back();
DQ.push_back(i);
}
}
cout << dp[n][K+1] << "\n"; recur(n,K+1);
sort(all(v)); v.pop_back(); for(auto u : v) cout << u << " ";
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int32_t main()':
sequence.cpp:34:24: warning: unused variable 'ans' [-Wunused-variable]
34 | cin >> n >> K; int ans = -(int)1e18;
| ^~~
# | 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... |