【hdu5527】【2015ACM/ICPC亚洲区长春站 】Too Rich

题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5527
题意&题解:
感觉自己真是弱啊,自己想的贪心是错的,根本没想到20,50的特判,╮(╯▽╰)╭
附我參考的聚聚的题解:
http://blog.csdn.net/snowy_smile/article/details/49592521
代码:
(差点儿和那位聚聚的一样。ORZ)

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
int val[11]={0,1,5,10,20,50,100,200,500,1000,2000};
int x,T,p,num[11],vis[11],use[11];
int ans=-1;
long long sum;
int solve()
{
    memset(use,0,sizeof(use));
    if (vis[5]>num[5] || vis[8]>num[8]) return 0;
    long long tmp=vis[5]*val[5]+vis[8]*val[8];
    int tmpn=vis[5]+vis[8];
    for (int i=1;i<=10;i++)
    {
        if (vis[i]==-1)
        {
            tmp+=num[i]*val[i];
            tmpn+=num[i];
            use[i]=num[i];
        }
        else
        {
            use[i]=num[i]-vis[i];
            if (use[i]%2==1) use[i]--;
            tmpn+=use[i];
            tmp+=use[i]*val[i];
        }
        if (tmp>=x)
        {
            long long chao=tmp-x;
            for (int j=i;j>=1;j--)
            {
                if (vis[j]==-1)
                {
                    long long tui=min((long long)use[j],chao/val[j]);
                    tmpn-=tui;
                    chao-=tui*val[j];               
                }
                else
                {
                    long long tui=min((long long)use[j],chao/val[j]);
                    if (tui%2==1) tui--;                    
                    tmpn-=tui;
                    chao-=tui*val[j];
                }
                if (chao==0)
                {
                    ans=max(ans,tmpn);
                    return 0;
                }
            }
            return 0;
        }
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&x);
        memset(vis,-1,sizeof(vis));     
        for (int i=1;i<=10;i++)
        {
            scanf("%d",&num[i]);
            sum+=num[i]*val[i];
        }
        if (sum<x)
        {
            printf("-1\n");
            continue;
        }
        ans=-1;
        vis[5]=0,vis[8]=0; solve();
        vis[5]=1,vis[8]=1; solve();
        vis[5]=1,vis[8]=0; solve();
        vis[5]=0,vis[8]=1; solve();
        printf("%d\n",ans);
    }
}
posted @ 2018-01-13 17:30  llguanli  阅读(113)  评论(0编辑  收藏  举报