제출 #161917

#제출 시각아이디문제언어결과실행 시간메모리
161917HungAnhGoldIBO2020수열 (APIO14_sequence)C++14
89 / 100
1371 ms81588 KiB
#include<bits/stdc++.h> using namespace std; const int N=1e5+2; const int K=201; int n,pre[K][N]; long long dp[2][N],sum[N]; stack<int> lis; void solve(int idx,int l,int r,int lef,int rig){ if(lef>rig){ return; } int i,mid=(lef+rig)>>1; long long max1=-1; for(i=min(mid-1,r);i>=l;i--){ if(max1<dp[(idx+1)&1][i]+(sum[mid]-sum[i])*(sum[n]-sum[mid])){ max1=dp[(idx+1)&1][i]+(sum[mid]-sum[i])*(sum[n]-sum[mid]); pre[idx][mid]=i; } } dp[idx&1][mid]=max1; solve(idx,l,pre[idx][mid],lef,mid-1); solve(idx,pre[idx][mid],r,mid+1,rig); } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int i,j,k,l,num; cin>>n>>num; for(i=1;i<=n;i++){ cin>>j; sum[i]=sum[i-1]+j; } for(i=1;i<=n;i++){ dp[1][i]=(sum[n]-sum[i])*sum[i]; pre[1][i]=i; } for(i=2;i<=num;i++){ solve(i,i-1,n-1,i,n-1); } j=n; for(i=num;i<=n;i++){ if(dp[num&1][j]<dp[num&1][i]){ j=i; } } cout<<dp[num&1][j]<<'\n'; for(i=num;i>0;i--){ lis.push(j); j=pre[i][j]; } while(lis.size()){ cout<<lis.top()<<' '; lis.pop(); } } /* 7 3 4 1 3 4 0 2 3 */

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'int main()':
sequence.cpp:27:10: warning: unused variable 'k' [-Wunused-variable]
  int i,j,k,l,num;
          ^
sequence.cpp:27:12: warning: unused variable 'l' [-Wunused-variable]
  int i,j,k,l,num;
            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...