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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值