이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define sc second
#define fr first
#define pb emplace_back
#define all(s) s.begin(), s.end()
#define sz(s) ( (int)s.size() )
#define Scan(a) scanf ("%I64d", &a)
#define scan(a) scanf ("%d", &a)
using namespace std;
const int N = (int)100007;
int n, k, arr[N], pr[N], par[N][207];
int dp[N][207];
int calc(int l,int r){
return pr[l] * (pr[r] - pr[l]);
}
void solve(int k,int l,int r,int optl,int optr){
if(l > r){
return;
}
int mid = (l + r) >> 1;
int opt = optl;
dp[mid][k] = 0;
for(int i = optl;i <= min(mid,optr); i++){
int cur = dp[i][k - 1] + calc(i,mid);
if(cur >= dp[mid][k]){
dp[mid][k] = cur;
opt = i;
}
}
par[mid][k] = opt;
solve(k,l,mid - 1,optl,opt);
solve(k,mid + 1,r,opt,optr);
}
main(){
cin >> n >> k;
for(int i = 1;i <= n; i++){
cin >> arr[i];
pr[i] = pr[i - 1] + arr[i];
}
if(n < 10000){
for(int g = 1;g <= k; g++){
for(int i = 1;i <= n; i++){
for(int j = 1;j < i; j++){
if(dp[i][g] <= dp[j][g - 1] + (pr[j] * (pr[i] - pr[j]))){
par[i][g] = j;
dp[i][g] = dp[j][g - 1] + (pr[j] * (pr[i] - pr[j]));
}
}
}
}
cout << dp[n][k] << endl;
vector<int > ans;
while(par[n][k]){
ans.pb(par[n][k]);
n = par[n][k];
k--;
}
for(int i = ans.size() - 1; i >= 0; i--){
cout << ans[i] << " ";
}
return 0;
}
for(int i = 1;i <= k; i++){
solve(i,1,n,1,n);
}
cout << dp[n][k] << '\n';
vector<int > ans;
while(par[n][k]){
ans.pb(par[n][k]);
n = par[n][k];
k--;
}
for(int i = ans.size() - 1; i >= 0; i--){
cout << ans[i] << " ";
}
}
/*
7 3
4 1 3 4 0 2 3
*/
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp:40:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# | 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... |