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

Is the Roborock Q5+...

Roborock Q5+ 6-Month Real World Verdict After 6 months of...

Best Replacement Parts, Filters,...

Best Replacement Bundles & Accessories for Roborock Q5+ Stocking up...

Roborock Q5+ Battery Life:...

Roborock Q5+ Battery Capacity & Range Review Equipped with a...

Can the Roborock Q5+...

Dark Room Navigation & Height Clearance Test Unlike camera-based robot...

How to Fix Roborock...

Troubleshooting: Clearing Roborock Q5+ Jams & Error Alerts If your...

Is the Roborock Q5+...

Roborock Q5+ Decibel Test & Noise Levels Noise levels vary...

Is the Roborock Q5+ Robot Vacuum Worth the Price? Real 6-Month Review

Roborock Q5+ 6-Month Real World Verdict After 6 months of daily automated cleaning in a busy household with pets, here is our final review of...

Best Replacement Parts, Filters, and Dustbags for the Roborock Q5+

Best Replacement Bundles & Accessories for Roborock Q5+ Stocking up on replacement dustbags and filters ensures continuous 7-week hands-free operation. 🛒 Check Roborock Parts on Amazon As...

Roborock Q5+ Battery Life: How Many Square Feet Can It Clean on One Charge?

Roborock Q5+ Battery Capacity & Range Review Equipped with a massive 5200 mAh Lithium-ion battery, the Q5+ is designed for large multi-room homes. 🛒 Check Roborock...

Can the Roborock Q5+ Navigate Dark Rooms and Under Low Furniture?

Dark Room Navigation & Height Clearance Test Unlike camera-based robot vacuums (which struggle in dark rooms), LiDAR technology uses invisible laser light. 🛒 Check Roborock Q5+...

How to Fix Roborock Q5+ Main Brush Tangles and Error Code Alerts

Troubleshooting: Clearing Roborock Q5+ Jams & Error Alerts If your Roborock app sends an "Error 5" or "Main Brush Jammed" notification, follow these simple fixes. 🛒...

Is the Roborock Q5+ Loud While Vacuuming and Auto-Emptying? Decibel Test

Roborock Q5+ Decibel Test & Noise Levels Noise levels vary depending on whether the robot is running on Quiet mode or self-emptying at its dock. 🛒...

How Often Do You Need to Replace Roborock Q5+ Dustbags, Filters, and Brushes?

Official Maintenance Schedule for Roborock Q5+ Keeping replacement components on a proper schedule ensures your Q5+ maintains peak 2700Pa suction. 🛒 Check Roborock Parts on Amazon As...

Roborock Q5+ Suction Power: How Does 2700Pa Handle Thick Rugs and Doormats?

2700Pa Suction Power & Carpet Boost Benchmark Deep carpet cleaning requires both high static pressure suction and brush agitation. 🛒 Check Roborock Q5+ Price on Amazon As...

How to Connect and Map Your Home with the Roborock App (LiDAR Navigation Guide)

Master the Roborock App & LiDAR 3D Mapping The Roborock App is widely considered the best smartphone app in the smart home vacuum category. 🛒 Check...