AC Challenge (ACM-ICPC 2018 Nanjing network game)

Topic: the Click
meaning of the questions: Given n topic, if you want AC i-th, you need to AC designated pj a problem, get a score of t * a [i] + b [i], ask the biggest score obtained is How many.
State property is MAX, but contain the condition, n a subject can be directly compressed state. But the two-dimensional dp T will obviously be overrun and memory, can be changed to a one-dimensional array.

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
ll a[21],b[21],c[21];
ll dp[(1<<20)];
ll solve(ll n)
{
    ll cnt=0;
    while(n)
    {
        if(n&1)
            cnt++;
        n>>=1;
    }
    return cnt;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    ll n,i,j,k;
    scanf("%lld",&n);
    for(i=1;i<=n;i++)
    {
        ll temp;
        scanf("%lld %lld %lld",&a[i],&b[i],&temp);
        ll sum=0;
        if(!temp)
        {
            dp[(1<<(i-1))]=a[i]+b[i];
            c[i]=0;
            continue;
        }
        for(j=0;j<temp;j++)
        {
            ll tmp;
            scanf("%lld",&tmp);
            sum+=(1<<(tmp-1));
        }
        c[i]=sum;
    }
    ll yyyy=(1<<n);
    for(i=0;i<yyyy;i++)
    {
        for(j=1;j<=n;j++)
        {
            if((((1<<(j-1))&i))||(c[j]&i)!=c[j])
                continue;
            if(dp[i]==-1)
                continue;
            ll temp=(1<<(j-1))|i;
            ll tmp=solve(temp);
            dp[temp]=max(dp[temp],dp[i]+a[j]*tmp+b[j]);
        }
    }
    ll ans=0;
    for(i=0;i<yyyy;i++)
    {
        ans=max(ans,dp[i]);
    }
    printf("%lld",ans);
    return 0;
}

Published 72 original articles · won praise 19 · views 7490

Guess you like

Origin blog.csdn.net/weixin_43958964/article/details/105224125