VK Cup 2015

来源:互联网 发布:mac 草图大师 卡 编辑:程序博客网 时间:2024/04/27 12:40
C. The Art of Dealing with ATM
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.

For example, if a country uses bills with denominations 10501005001000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.

Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.

Input

The first line contains two integers nk (1 ≤ n ≤ 50001 ≤ k ≤ 20).

The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order.

The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make.

The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.

Output

For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.

Examples
input
6 2010 50 100 500 1000 5000842001000009500096000990001010020159950
output
6201920-13-1-1
input
5 21 2 3 5 8813579111315
output
1112222-1

题意:给n种面值的钱币,然后有q个询问,每次询问给出一个x,问说能不能在不超过k张钱币,不超过2种面值的情况下,凑出x块钱,如果能输出最少的钱币数,如果不能输出-1;

第一眼不知道为什么我居然想用背包做,然后T了,然后又暴力了一下,又T了,然后又写了一个姿势很优雅的二分查找,还是T了。最后用map瞎搞搞出来,先n*k把一个面值的钱凑算出来,map[]存这个钱数所对应的最少钱币张数,然后对于每个金额y我们查询一下y - x ,然后找钱币数最少的那个就行。

#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<set>#include<map>#include<time.h>#include<cstdio>#include<vector>#include<stack>#include<queue>#include<iostream>using namespace std;#define  LONG long longconst int   INF=0x3f3f3f3f;const int   MOD=1e9+7;const double PI=acos(-1.0);#define clrI(x) memset(x,-1,sizeof(x))#define clr0(x) memset(x,0,sizeof x)#define clr1(x) memset(x,INF,sizeof x)#define clr2(x) memset(x,-INF,sizeof x)#define EPS 1e-10int a[6000];int n ;int K ;int main(){    cin>>n>>K;    for(int i =1; i<= n ;++i)        scanf("%d",&a[i]);    sort(a + 1 , a + n + 1);    int q;    map <int,int  > mp;    mp.clear() ;    for(int i  =1; i <=n ;++ i)        for(int j = 0 ; j <= K ;++ j)            if(mp[a[i]*j] == 0)                mp[a[i] * j] = j;            else                mp[a[i] * j] = min(mp[a[i] * j] , j);    cin>>q;    while(q--)    {        int x ; cin>>x;        int ans = INF;        for(int i =1; i<=n ;++ i)            for(int j = 0; j <= K; ++ j)                if(mp[x - a[i] * j ]!= 0 || x == a[i] * j )                    ans = min(ans , j + mp[x - a[i] * j]);        if(ans > K)            cout<<-1<<endl;        else cout<<ans<<endl;    }}


1 0
原创粉丝点击