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

RFK Jr.’s top drug...

Top FDA Regulator Resigns Amid Scandal Involving 'Revenge Campaign' The...

National Guard, DC Police...

National Guard Deployed as Chaos Grips DC Navy Yard...

She’s a pop star,...

The Unlikely Parallel: Why Katy Perry and Justin Trudeau...

Government issuing license for...

Government Formalizes Real Estate Sector, Begins Issuing Operating Licenses...

Yokogawa to Deliver Integrated...

Yokogawa Powers Green Riyadh: Integrated Control Systems to Drive...

Neurogen Biomarking: A Premium,...

Neurogen Biomarking: Redefining Proactive Brain Health with an Impact-Focused...

RFK Jr.’s top drug regulator resigns after ‘revenge campaign’ against ex-colleague: report

Top FDA Regulator Resigns Amid Scandal Involving 'Revenge Campaign' The political and regulatory landscape was shaken this week following the abrupt resignation of a senior...

National Guard, DC Police Respond As Dozens Descend On Navy Yard, Several Arrests Made

National Guard Deployed as Chaos Grips DC Navy Yard on Halloween Night Halloween night traditionally brings costumed fun, but in Washington D.C.’s Navy Yard neighborhood,...

She’s a pop star, he’s a former PM – why Katy Perry and Justin Trudeau just might work

The Unlikely Parallel: Why Katy Perry and Justin Trudeau Share a Volatile Path In the often-unpredictable world of celebrity and politics, finding common ground between...

Government issuing license for real estate business

Government Formalizes Real Estate Sector, Begins Issuing Operating Licenses in Kathmandu Announcing a significant regulatory shift, the Department of Land Management and Archives has confirmed...

Yokogawa to Deliver Integrated Control Systems for Urban Infrastructure in Green Riyadh Project

Yokogawa Powers Green Riyadh: Integrated Control Systems to Drive Massive Afforestation Project The Kingdom of Saudi Arabia is aggressively pursuing massive urban transformation initiatives, and...

Neurogen Biomarking: A Premium, Impact Focused Model for Proactive Brain Health

Neurogen Biomarking: Redefining Proactive Brain Health with an Impact-Focused Model In an era where technology constantly advances our understanding of the human body, one area...

Bank holiday today: Are banks open or closed on Tuesday, October 28 for Chhath Puja? Check here

Banks Closed on October 28 for Chhath Puja: What Customers Need to Know If you live in Bihar or Jharkhand, take note: banks in key...

VS Code is an open-source platform these days, not just a development tool

VS Code: The Evolution from Code Editor to Open-Source Platform For years, Visual Studio Code (VS Code) was celebrated as a lightweight yet incredibly powerful...

October Singer Brand Reputation Rankings Announced

K-Pop Dominates: October Singer Brand Reputation Rankings Revealed The highly anticipated monthly metrics are here! The Korean Business Research Institute has released its comprehensive data...