zoukankan      html  css  js  c++  java
  • cf524C 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.

    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

    因为只能取2种面值,所以枚举一种面值,然后搞个map找另一个面值跟它匹配

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<deque>
     9 #include<set>
    10 #include<map>
    11 #include<ctime>
    12 #define LL long long
    13 #define inf 0x7ffffff
    14 #define pa pair<int,int>
    15 #define mkp(a,b) make_pair(a,b)
    16 #define pi 3.1415926535897932384626433832795028841971
    17 using namespace std;
    18 inline LL read()
    19 {
    20     LL x=0,f=1;char ch=getchar();
    21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    23     return x*f;
    24 }
    25 int n,k,q,cnt;
    26 int a[5010];
    27 struct p{int v,w,k;}b[100010];
    28 bool operator <(p a,p b){return a.w<b.w||a.w==b.w&&a.k<b.k;}
    29 map<int,pa>mp,mp2;
    30 int main()
    31 {
    32     mp.clear();mp2.clear();
    33     n=read();k=read();
    34     for (int i=1;i<=n;i++)
    35     {
    36         a[i]=read();
    37         for (int j=1;j<=k;j++)
    38         b[++cnt].v=i,b[cnt].k=j,b[cnt].w=j*a[i];
    39     }
    40     sort(b+1,b+cnt+1);
    41     for (int i=1;i<=cnt;i++)
    42     {
    43         int w=b[i].w,kk=b[i].k,v=b[i].v;
    44         if (mp[w]==mkp(0,0))mp[w]=mkp(kk,v);
    45         else
    46         {
    47             if (mp[w].first>kk)mp2[w]=mp[w],mp[w]=mkp(kk,v);
    48             else if (mp2[w].first>kk)mp2[w]=mkp(kk,v);
    49         }
    50     }
    51     q=read();
    52     for (int i=1;i<=q;i++)
    53     {
    54         int x=read(),ans=21;
    55         for (int j=1;j<=n;j++)
    56         {
    57             for (int l=1;l<=k;l++)
    58             {
    59                 int now=l*a[j];
    60                 if (now>x)break;
    61                 if (now==x){ans=min(ans,l);break;}
    62                 pa s=mp[x-now];
    63                 if (s.second==j)s=mp2[x-now];
    64                 if (s==mkp(0,0))continue;
    65                 ans=min(ans,s.first+l);
    66             }
    67         }
    68         printf("%d
    ",ans>k?-1:ans);
    69     }
    70 }
    cf524C
  • 相关阅读:
    .net实现支付宝在线支付
    彻头彻尾理解单例模式与多线程
    Linq中的Select与Select many
    MVC中子页面如何引用模板页中的jquery脚本
    浅谈MemCahe
    左连接,右连接,内连接(left join ,right join,inner join)
    协变与逆变
    子类对父类中的属性和字段的改写
    里氏转换
    MVC基础篇—控制器与视图数据的传递
  • 原文地址:https://www.cnblogs.com/zhber/p/7284564.html
Copyright © 2011-2022 走看看