2018校赛I题

A Love Story



Description


In order to make the girlfriend happy,CZY decided to send flowers to CM every week. CZY has some coins, a total of N (1 <= N <= 20) different denominations. Every denomination can divide all the denominations that are bigger than it. He wants to use this set of coins to buy flowers per week. The cost of the flowers is at least C(1 <= C <= 100000000), and the shop won't give change. Please help him figure out how many weeks he can send flowers to his girlfriend.


Input


There are multiple sets of input and output,Each set contains:

Line 1: two integers separated by space: N and C  

second to line N+1: each row has two integers representing a denomination of coins: coin denomination V (1 <= V <= 100000000) and the number of coins of that denomination that CZY has, B (1 <= B <= 1000000).


Output


A single integer, means that CZY can send flowers at most weeks.


Sample Input 1

3 6 10 1 1 100 5 120

Sample Output 1

111


Hint



CZY can pay a 10 cent coin in a week. Then he pays two 5 cent coins every week for the next 10 weeks. In the last 100 weeks, he pays a 1 cent coin and a 5 cent coin.


不多说了,直接放代码,每次尽量去掉其中一种,最多两次去完,每次搜索得出去掉什么去掉多少然后统计。
比题解速度要快一点,到底是菜啊,题都没看清楚。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct{int l,r;}node;
node a[25];
int n,c,v,b,cnt,num,vis[25];
long long ans;
bool cmp(node a,node b)
{
    return a.l>b.l;
}
void dfs(int now,int sum)
{
    if(now>=cnt)
    {
        if(sum<c)
        {
            int i;
            for(i=cnt-1;i>=0;i--)
                if(a[i].r>vis[i])
                {
                    vis[i]++;
                    break;
                }
            if(i<0)
                return;
        }
        int tp=0x3f3f3f3f;
        for(int i=0;i<cnt;i++)
            if(vis[i])
                tp=min(tp,a[i].r/vis[i]);
        for(int i=0;i<cnt;i++)
            a[i].r-=tp*vis[i];
        ans+=tp;
        return;
    }
    int tp=min((c-sum)/a[now].l,a[now].r);
    vis[now]=tp;
    dfs(now+1,sum+tp*a[now].l);
}
int main()
{
    while(scanf("%d%d",&n,&c)!=EOF)
    {
        ans=0;
        cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&v,&b);
            if(v>=c)
                ans+=b;
            else
            {
                a[cnt].l=v;
                a[cnt++].r=b;
            }
        }
        sort(a,a+cnt,cmp);
        for(int i=0;i<2*cnt;i++)
        {
            num=0;
            dfs(0,0);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fanbaobao829/article/details/79959317
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭