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];
}

7 Reasons This Coffee...

📢 As an Amazon Associate, I earn from qualifying...

Why Kitchen Organization Tools...

📢 As an Amazon Associate, I earn from qualifying...

Is Dorm Room Hacks...

📢 As an Amazon Associate, I earn from qualifying...

Stop Buying Cheap Alternatives...

📢 As an Amazon Associate, I earn from qualifying...

Everything You Need to...

📢 As an Amazon Associate, I earn from qualifying...

Why Bathroom Organization Hacks...

📢 As an Amazon Associate, I earn from qualifying...

7 Reasons This Coffee Bar Accessories Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Why Kitchen Organization Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Is Dorm Room Hacks Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Stop Buying Cheap Alternatives This Camping Essentials Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Everything You Need to Know About Outdoor Patio Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Bathroom Organization Hacks Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Electric Cleaning Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Electric Cleaning Tools?The Electric Cleaning Tools...

Everything You Need to Know About Portable Cooling Gear Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Portable Cooling Gear?The Portable Cooling Gear...

China’s largest memory chipmaker sparks fears of a cash drain before blockbuster IPO

China's semiconductor ambitions are facing a fresh financial dilemma as ChangXin Memory Technologies (CXMT), the nation's premier DRAM chipmaker, prepares for a highly anticipated...