이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define all(x) (x).begin() , (x).end()
#define pll pair<long long, long long>
#define fi first
#define se second
#define bit(i,j) ((j >> i) & 1)
using namespace std;
#define int long long
const long long inf = 1e18+1;
const int mod = 998244353;
const int MAXN = 100000;
int x[MAXN+10] , pre[MAXN + 10];
int dp[MAXN+10] , lst[MAXN+ 10] ;pll trace[MAXN+10][210];
void compute(int l , int r , int optl , int optr , int k){
if(l > r) return;
int m = l + r >> 1;
pll res = {-1e18 , -1};
for(int i = optl ; i <= min(m , optr) ; i++){
int t = lst[i-1] + ((pre[m] - pre[i-1]) * pre[i-1]);
res= max(res , {t , i});
}
trace[m][k] = {res.se-1 , k-1};
dp[m] = res.fi;
int opt = res.se;
compute(l , m-1 , optl , opt , k);
compute(m + 1 , r , opt, optr , k);
}
void solve(){
int n , k; cin >> n >> k;
for(int i = 1 ; i <= n ; i++){
cin >> x[i];
pre[i] = pre[i-1] + x[i];
}
for(int i = 1 ; i <= n ; i++) lst[i] = -inf;
lst[0] = 0;
for(int i = 1 ; i <= k+1; i++){
compute(1 , n , 1 , n , i);
for(int i = 0 ; i <= n ; i ++) lst[i] = dp[i];
}
cout << lst[n] << "\n";
pll t = {n , k+1};
vector<int> ans;
while(t.se != 0){
if(trace[t.fi][t.se].fi != 0) cout << trace[t.fi][t.se].fi << " ";
t = trace[t.fi][t.se];
}
cout << "\n";
}
int32_t main(){
//freopen("PAT.INP", "r", stdin);
//freopen("PAT.OUT", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void compute(long long int, long long int, long long int, long long int, long long int)':
sequence.cpp:20:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
20 | int m = l + r >> 1;
| ~~^~~
# | 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... |