VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E---The Art of Dealing with ATM

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 10, 50, 100, 500, 1000 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 n, k (1 ≤ n ≤ 5000, 1 ≤ 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.
Sample test(s)
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

最多只能用2中面额的,用一个map搞搞就行了

/************************************************************************* > File Name: CF-VK-E.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年03月23日 星期一 16时30分35秒 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

vector <PLL> data;
map <int, int> mp;

int main()
{
    int n, k;
    while (~scanf("%d%d", &n, &k))
    {
        data.clear();
        mp.clear();
        int val;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &val);
            for (int j = 1; j <= 20; ++j)
            {
                data.push_back ( make_pair(val * j, j) );
                if (mp.find(val * j) == mp.end())
                {
                    mp[val * j] = j;
                }
                else
                {
                    mp[val * j] = min (mp[val * j], j);
                }
            }
        }
        sort(data.begin(), data.end());
        int m;
        scanf("%d", &m);
        while (m--)
        {
            scanf("%d", &val);
            int size = data.size();
            int ans = inf;
            for (int i = 0; i < size; ++i)
            {
                int now = data[i].first;
                int cnt1 = data[i].second;
                if (now == val)
                {
                    ans = min(ans, cnt1);
                }
                if (mp[val - now])
                {
                    ans = min(ans, mp[val - now] + cnt1);
                }
            }
            if (ans > k)
            {
                ans = -1;
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}

你可能感兴趣的:(VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E---The Art of Dealing with ATM)