Sponsors

Codeforces Round #384 (Div. 2), problem: (E) Vladik and cards Solution in C/C++

#include <stdio.h>
#include <string.h>
#define MIN(X,Y) ((X)<(Y) ? (X) : (Y))

int n,c[1010],r[1010][10][1010],dp[10][300][1010];
int solve(int aver,int c,int b,int k);

int main(void)
{
    //freopen("cards.in","r",stdin);
    //freopen("cards.out","w",stdout);
    scanf("%d",&n);
    int i,j,k;
    for(i=1;i<=n;i++)
        scanf("%d",&c[i]);
    memset(r,-1,sizeof(r));
    int s;
    for(i=1;i<=n;i++)
        for(j=1;j<=8;j++)
        {
            r[i][j][s=0]=i-1;
            for(k=i;k<=n;k++)
                if(c[k]==j) r[i][j][++s]=k;
        }
    int min=1,max=n,m,t;
    while(min<max)
    {
        m=(min+max+1)/2;
        memset(dp,-1,sizeof(dp));
        if(solve(m/8,m%8,0,1)<=n) min=m;
        else max=m-1;
    }
    printf("%d\n",min);
    return 0;
}

int solve(int aver,int c,int b,int k)
{
    if(dp[c][b][k]>=0) return dp[c][b][k];
    int t=(1<<8)-1;
    if( (t&b)==t && !c ) return dp[c][b][k]=k-1;
    else if((t&b)==t) return dp[c][b][k]=n+1;
    dp[c][b][k]=n+1;
    int i;
    for(i=0;i<8;i++)
        if((b&(1<<i))==0)
        {
            if(r[k][i+1][aver]>=0)
                dp[c][b][k]=MIN(dp[c][b][k],solve(aver,c,b|(1<<i),r[k][i+1][aver]+1));
            if( c && r[k][i+1][aver+1]>=0 )
                dp[c][b][k]=MIN(dp[c][b][k],solve(aver,c-1,b|(1<<i),r[k][i+1][aver+1]+1));
        }
    return dp[c][b][k];
}

You can still save...

Score Big Savings on Steelcase Furniture: Extended Labor Day...

Apple has reportedly launched...

Apple's Internal AI Chatbot: A Retail-Focused Approach Apple, a company...

Solana’s U.S. backing vs....

Solana's U.S. Backing vs. Binance's Asian Capital: A Crypto...

Why Trump Won’t Tell...

Why Trump Won't Tell the Truth About His Health:...

Mapping the Jazz Faculty...

Mapping the Jazz Faculty Gender Gap: A Berklee-Sponsored Study...

Burning Man festivalgoer is...

Burning Man Festivalgoer Found Dead in Pool of Blood;...

You can still save 20% off Steelcase office chairs and desks during this extended Labor Day sale

Score Big Savings on Steelcase Furniture: Extended Labor Day Sale! Labor Day may be over, but the deals aren't! Steelcase, a renowned name in...

Apple has reportedly launched an AI chatbot, but it’s not for the iPhone 17 (or you)

Apple's Internal AI Chatbot: A Retail-Focused Approach Apple, a company synonymous with consumer-facing technology, has reportedly launched its own AI chatbot. However, unlike the...

Solana’s U.S. backing vs. Binance’s Asian capital: Who will win this battle?

Solana's U.S. Backing vs. Binance's Asian Capital: A Crypto Clash for Supremacy The cryptocurrency landscape is a volatile battlefield, and a fascinating clash is brewing...

Why Trump Won’t Tell the Truth About His Health: Michael Wolff | Inside Trump’s Head

Why Trump Won't Tell the Truth About His Health: Decoding Michael Wolff's Insights Michael Wolff, a controversial yet impactful figure in political journalism, recently offered...

Mapping the Jazz Faculty Gender Gap

Mapping the Jazz Faculty Gender Gap: A Berklee-Sponsored Study Reveals Stark Imbalance A recently published study sponsored by Berklee College of Music paints a concerning...

Burning Man festivalgoer is found dead in pool of blood as cops launch murder probe

Burning Man Festivalgoer Found Dead in Pool of Blood; Murder Investigation Launched A tragic incident has cast a shadow over the iconic Burning Man festival...

Fleischmann says ‘never’ to town hall (Ruby Rayner/timesfreepress.com)

Fleischmann Declares 'Never' to Town Halls, Calling Them 'Left-Wing Bait' Chattanooga Congressman Chuck Fleischmann has emphatically ruled out holding town hall meetings, labeling them as...

Plastic pollution poses growing health threat as Malaysia eyes Extended Producer Responsibility law

Plastic Pollution: A Growing Health Threat in Malaysia Malaysia is grappling with a significant environmental and health crisis: plastic pollution. A recent report highlights...

Is A Battery-Powered Cooler Like The Anker SOLIX EverFrost 2 Worth It?

Is a Battery-Powered Cooler Like the Anker SOLIX EverFrost 2 Worth It? The summer heat is on, and keeping your drinks refreshingly cold can be...