VK Cup 2015 - Round 1 C. The Art of Dealing with ATM

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 20
10 50 100 500 1000 5000
8
4200
100000
95000
96000
99000
10100
2015
9950
output
6
20
19
20
-1
3
-1
-1
input
5 2
1 2 3 5 8
8
1
3
5
7
9
11
13
15
output
1
1
1
2
2
2
2
-1

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

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

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define  LONG long long
const 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-10
int 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  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<


你可能感兴趣的:(CF,贪心)