이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=2e3+9;
int a[maxn];
int f[maxn][maxn];
int mx[maxn][maxn];
int n,k;
int cal(int l,int r){
if (l>r)return 0;
if (f[l][r]!=-1)return f[l][r];
int pos=mx[l][r];//min pos
int ans=0;
for1(i,max(1,pos-k+1),min(pos,n-k+1)){//just <=n
int l1=max(i,l),r1=min(r,i+k-1);
ans=max(ans,cal(l,i-1)+cal(r1+1,r)+(r1-l1+1)*a[pos]);//dp to split into 2 array that min all array >=a[pos]
}
return f[l][r]=ans;
}
void sub4(){
for1(i,1,n){
mx[i][i]=i;
for1(j,i+1,n){
mx[i][j]=mx[i][j-1];
if (a[j]<a[mx[i][j]])mx[i][j]=j;
}
}
for1(i,1,n){
for1(j,1,n){
f[i][j]=-1;
}
}
cout<<cal(1,n);
}
int dp[maxn];
void sub1(){
for1(i,1,n)dp[i]=-1;
for1(i,0,n-1){
if (dp[i]<0)continue;
for1(j,max(i+1-k+1,1),min(i+1,n-k+1)){
dp[j+k-1]=max(dp[j+k-1],dp[i]+(j+k-1-i)*a[i+1]);
}
}
cout<<dp[n];
return;
}
void sub2(){
for1(i,1,n)dp[i]=-1;
for1(i,0,n-1){
if (dp[i]<0)continue;
if (a[i+1]==2){
dp[i+1]=max(dp[i+1],dp[i]+2);
}
for1(j,i+1+k-1,n){
dp[j]=max(dp[j],dp[i]+(j-(i+1)+1));
}
}
cout<<dp[n];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
freopen("c.INP","r",stdin);
freopen("c.ANS","w",stdout);
cin>>n>>k;
for1(i,1,n)cin>>a[i];
bool fl=true;
for1(i,1,n){
if (a[i]<=2)continue;
fl=false;
break;
}
if (fl){
sub2();
return 0;
}
fl=true;
for1(i,2,n){
if (a[i-1]<=a[i])continue;
fl=false;
break;
}
if (fl){
sub1();
return 0;
}
sub4();
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen("c.INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
sequence.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen("c.ANS","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |