题目大意

ATM取款机有n种不同的钱币kind[i],每次取款允许吐出不超过k张钱币,且钱币的种类数不能超过2(一开始没理解2的意思),现在有q次取款,钱数为ques,问ATM能否凑出这样的钱,若能的话吐出的钱币数最少是多少。

n <= 5000, k <= 20, q <= 20, kind[i] <= 10 ^ 7, ques <= 2 * 10 ^ 8。

题解:

用两种钱找零可以写成a * x + b * y = ques,其中x和y是两种钱币、a和b是两种钱币分别用的钱数、ques是所需凑出的钱数,则最优的解法是枚举x, a, b,判断y是否在钱的种类中,主要x或者y可以为0;

一开始想的用搜索,果断试了一下,果然超时;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#define maxn 1001
#define INF ~0u >> 1
using namespace std;
int n,k,vist[10000004],kind[6000];
int ans;
int main()
{
int q,ques;
scanf("%d %d",&n,&k);
memset(vist,0,sizeof(vist));
for(int i=1; i<=n; i++)
{
scanf("%d",&kind[i]);
vist[kind[i]]=1;

}
scanf("%d",&q);
for(int ii=1; ii<=q; ii++)
{
scanf("%d",&ques);
ans=INF;
for(int i=0; i<=k; i++)
{
for(int j=0; j+i<=k; j++)
{
for(int t=1; t<=n; t++)
{
if(j==0)
{
if(kind[t]*i==ques)
ans=min(ans,i);
}
else
{
int b=(ques-kind[t]*i)/j;
if(b>0&&b<=10000000&&vist[b]&&kind[t]*i+b*j==ques)
ans=min(ans,i+j);
}

}
}
}
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}

return 0;
}

codeforce The Art of Dealing with ATM的更多相关文章

  1. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

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

  2. 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 mo ...

  3. CodeForces 524C The Art of Dealing with ATM (二分)

    题意:给定 n 种不同的钞票,然后用q个询问,问你用最多k张,最多两种不同的钞票能不能组成一个值. 析:首先如果要求的值小点,就可以用DP,但是太大了,所以我们考虑一共最多有n * k种钞票,如果每次 ...

  4. Codeforces 524C.The Art of Dealing with ATM(暴力)

    我先采用了智障解法(n * n枚举...刚开始把n看成1000了还以为能过) 理所当然的t了,不过我怀疑优化一下能过?(感觉数据不太行的亚子 然后就是O(n * k * k)的解法,看到好多人快乐二分 ...

  5. [转] Loren on the Art of MATLAB

    http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...

  6. 1179: [Apio2009]Atm

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1629  Solved: 615[Submit][Status ...

  7. The Art of Deception

    前言 一些黑客毁坏别人的文件甚至整个硬盘,他们被称为电脑狂人(crackers)或计算机破坏者(vandals).另一些新手省去学习技术的麻烦,直接下载黑客工具侵入别人的计算机,这些人被称为脚本小子( ...

  8. 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)

    说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...

  9. How the problem solved about " Dealing with non-fast-forward errors"

    Recently ,I got confused When I use  git to push one of my project. The problem is below: And I Foun ...

随机推荐

  1. SendMessage函数的常用消息及其应用大全

    来源:http://www.360doc.com/content/09/0814/10/19147_4907488.shtml,非常全面的解释. 文本框控件通常用于输入和编辑文字.它属于标准 Wind ...

  2. linux 常用命令;

    bc 计算器 :quit 退出 top 任务管理器 q退出 who 显示在线用户 whoami 当前操作用户 kill id 结束当前进程 ifconfig 显示当前网络状态 clear 清屏 vim ...

  3. ubuntu 软件安装的几种方法

    说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...

  4. Facebook技术架构

    Facebook MySQL,Multifeed (a custom distributed system which takes the tens of thousands of updates f ...

  5. C#窗体的加载等待(BackgroundWorker控件)实现

    窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...

  6. tab模块

    #!/usr/bin/env python # python startup file import sys import readline import rlcompleter import ate ...

  7. CodeForces 468A Program F

    Description Little X used to play a card game called "24 Game", but recently he has found ...

  8. C++,1....n中随机等概率的输出m个不重复的数(假设n远大于m)。

    #include <stdlib.h> #include <time.h> knuth(int n, int m) { srand((unsigned )); ; i < ...

  9. PHP及Javascript 正则判断中文(转)

    UTF-8匹配: 在javascript中,要判断字符串是中文是很简单的.比如: var str = "php编程"; if (/^[\u4e00-\u9fa5]+$/.test( ...

  10. ajax上传组件

    BJUI框架的异步上传组件功能. 初始化: 1.Data属性:div添加属性data-toggle="upload"后可触发上传组件. 示例代码: <div class=&q ...