이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vec vector<long long>
#define pb push_back
#define sz(a) a.size()
#define fi first
#define se second
#define ret(a) return cout<<(a)<<"\n",void()
#define endl "\n"
#define el cout<<"\n"
#define f(a) for(int i=0;i<(a);i++)
#define f1(a) for(int i=1;i<(a);i++)
#define all(v) v.begin(),v.end()
template<class T> void PRINTARR(T a[], int n){for(int i=0;i<n;i++){std::cout<<a[i]<<" ";}std::cout<<'\n';}
template<class T> void PRINTVEC(std::vector<T> arr){for(int i=0;i<arr.size();i++){std::cout<<arr[i]<<" ";}std::cout << '\n';}
int tc=1;
void input(){
}
const ll oo = 1e18;
const int maxn = 1e5+10;
const int mod = 1e9+7;
const int S = 360;
//REMEMBER:
//USE LONG LONG WHEN NECESSARY
//ALWAYS USE "\n" OR el;
//PRAGMA?
ll dp[2][maxn];
int trace[203][maxn];
int n,k;
vector<int> pf,a;
void cal(int cur, int l, int r, int otpl, int otpr){
if(l>r) return;
int mid=(l+r)/2;
int otp;
for(int k=min(mid,otpr);k>=otpl;k--){
// update(k,mid);
ll x = dp[0][k-1] + (pf[mid] - pf[k-1])*(pf[n] - pf[mid]);
if(x>=dp[1][mid]){
dp[1][mid]=x;
trace[cur][mid]=k-1;
otp=k;
}
}
cal(cur, l,mid-1,otpl,otp);
cal(cur, mid+1,r,otp,otpr);
}
void solve()
{
cin>>n>>k;
a.resize(n+1);
pf.resize(n+1);
f1(n+1){
cin>>a[i];
pf[i] = a[i];
pf[i] += pf[i-1];
}
for(int i=0;i<203;i++){
for(int j=0;j<maxn;j++) trace[i][j]=-1;
}
for(int i=1;i<=n;i++){
dp[0][i] = pf[i]*(pf[n]-pf[i]);
trace[1][i] = 0;
}
for(int i=2;i<=k+1;i++){
cal(i,1,n,1,n);
for(int j=0;j<=n;j++){
dp[0][j]=dp[1][j];
dp[1][j]=0;
}
}
cout<<dp[0][n]<<"\n";
int xx = n, yy=k;
vector<int> sus;
while(trace[yy][xx]!=-1){
// cout<<trace[yy][xx]+1<<" ";
sus.pb(trace[yy][xx]+1);
xx=trace[yy][xx];
yy--;
}
reverse(all(sus));
for(auto x:sus)cout<<x<<" ";
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
while (tc--)
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void cal(int, int, int, int, int)':
sequence.cpp:55:5: warning: 'otp' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | cal(cur, l,mid-1,otpl,otp);
| ~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |