Submission #1091822

# Submission time Handle Problem Language Result Execution time Memory
1091822 2024-09-22T09:53:29 Z modwwe Robots (APIO13_robots) C++17
0 / 100
2 ms 348 KB
#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("test.inp","r",stdin);
    freopen("test.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();
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen("test.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     freopen("test.ans","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -