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 00 seconds, and at most 11 hour. If a button press would result in a cooking time of less than 00 seconds, 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 11 hour. Initially, the microwave will run for 00 seconds. 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)(1≤n≤16,0≤t≤3600): 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)(−3600≤bi≤3600): 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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
typedef pair<int,int> P;
int main(){
int tn;
cin>>tn;
while(tn--){
int n,t;
scanf("%d%d",&n,&t);
int a[20];
for(int i=0;i<n;i++) scanf("%d",&a[i]);
if(t==0){
printf("0 0\n");
continue;
}
queue<P> que;
que.push(P{0,0});
int v[10000]={0};
int cnt[10000]={0};
for(int i=0;i<8000;i++) cnt[i]=10000;
while(que.size()){
P p=que.front();
que.pop();
int num=p.first;
if(v[num]) continue;
v[num]=1;
for(int i=0;i<n;i++){
int fz=num+a[i];
if(fz<0) fz=0;
if(fz>3600) fz=3600;
int len=p.second;
cnt[fz]=min(cnt[fz],len+1);
que.push(P{fz,cnt[fz]});
}
}
int minn=10000,len=0;
for(int i=t;i<8000;i++){
if(v[i]==1){
minn=i-t;
len=cnt[i];
break;
}
}
cout<<len<<" "<<minn<<endl;
}
return 0;
}