ACM Button Bashing

题目:

You recently acquired a new microwave, and noticed that it provides a large number of buttons to be able to quickly specify the time that the microwave should be running for. There are buttons both for adding time, and for subtracting time. You wonder how efficient you can be when entering cooking times: you want to minimize the number of required button presses.

The microwave can be running for at least 00seconds, and at most 11 hour. If a button press would result in a cooking time of less than 00seconds, the microwave will set the cooking time to 00 seconds. If a button press would result in a cooking time of more than 11 hour, the microwave will set the cooking time to 11hour. Initially, the microwave will run for 00seconds. There will always be a button adding at least 11 second to the cooking time.

Given the buttons that the microwave provides for entering cooking times, determine the least amount of button presses required to let the microwave run for a certain amount of time. If it is not possible to enter the desired cooking time precisely, determine the smallest achievable cooking time above the target, and the minimum number of button presses required for that cooking time, instead. The microwave does not allow to adjust the cooking time once it has started cooking.

Input Format

On the first line one positive number: the number of test cases, at most 100100. After that per test case:

  • one line with two space-separated integers nn and tt (1 \le n \le 16, 0 \le t \le 3600)(1n16,0t3600): the number of buttons available to change the cooking time, and the desired cooking time in seconds, respectively.
  • one line with nn space-separated integers b_ibi (-3600 \le b_i \le 3600)(3600bi3600): the number of seconds added to the cooking time when button ii is pressed.

Output Format

Per test case:

  • one line with two space-separated integers: the minimum number of button presses required to reach the required cooking time, and the minimum number of extra seconds that the microwave must be running for, respectively.
样例输入
2
3 50
-10 10 60
1 50
20
样例输出
2 0
3 10


题意:

给出微波炉需要的定时,以及n个不同的按钮,不同的按钮对应不同的效果,增加或者减少一些时间,当然微波炉最多运行一个小时;

分析:首先想到用dp做,因为最近一直在学dp,但是发现有负数,不好判断,还是bfs吧;

要注意的点就是当时间<0或者>3600时,一定要写成=0或者=3600;

第二点,book数组定义时一定要定义为一个非常大的数,分析见代码;

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
    int i,j,n,m,k,num[20],book[4000];
    scanf("%d",&k);
    while(k--)
    {
        queue<int>q;
        memset(book,INF,sizeof(book));
        scanf("%d %d",&n,&m);
        for(i=1;i<=n;i++)
            scanf("%d",&num[i]);
        q.push(0);
        book[0]=0;
        while(!q.empty())
        {
            int head=q.front();
            q.pop();
            for(i=1;i<=n;i++)
            {
               int tail=head+num[i];
                if(tail<0)
                    tail=0;
                else if(tail>3600)
                    tail=3600;
                if(book[tail]<=book[head]+1)由于判断条件是<=,因此一定要初始化为一个非常大的数
                    continue;
                q.push(tail);
                book[tail]=book[head]+1;
            }
        }
        int t;
        for(t=m;t<=3600;t++)//这个是从以为大佬那里学来的
        {
            if(book[t]!=INF)
                break;
        }
        printf("%d %d\n",book[t],t-m);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值