题意:给定 n 种不同的钞票,然后用q个询问,问你用最多k张,最多两种不同的钞票能不能组成一个值。

析:首先如果要求的值小点,就可以用DP,但是太大了,所以我们考虑一共最多有n * k种钞票,如果每次都挨着遍历,时间肯定受不了,

所以我们可以枚举其中一种,然后再用二分查找快速查找另一种,然后不断更新答案。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<double, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const double inf = 0x3f3f3f3f3f3f;
  30. const double PI = acos(-1.0);
  31. const double eps = 1e-5;
  32. const int maxn = 100 + 10;
  33. const int mod = 1e6;
  34. const int dr[] = {-1, 0, 1, 0};
  35. const int dc[] = {0, 1, 0, -1};
  36. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  37. int n, m;
  38. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  39. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. inline bool is_in(int r, int c){
  41. return r >= 0 && r < n && c >= 0 && c < m;
  42. }
  43. vector<int> v[25];
  44.  
  45. int main(){
  46. scanf("%d %d", &n, &m);
  47. for(int i = 0; i < n; ++i){
  48. int x;
  49. scanf("%d", &x);
  50. for(int j = 1; j <= m; ++j) v[j].push_back(x * j);
  51. }
  52. for(int i = 1; i <= m; ++i) sort(v[i].begin(), v[i].end());
  53. int q;
  54. scanf("%d", &q);
  55. while(q--){
  56. int x;
  57. scanf("%d", &x);
  58. int ans = INF;
  59. if(x == 0){ printf("0\n"); continue; }
  60. for(int i = 1; i <= m; ++i)
  61. for(int j = 0; j < v[i].size(); ++j){
  62. int y = x - v[i][j];
  63. if(y == 0){ ans = min(ans, i); break; }
  64. if(y < 0) break;
  65. for(int k = 1; k <= m - i; ++k){
  66. int pos = lower_bound(v[k].begin(), v[k].end(), y) - v[k].begin();
  67. if(pos < v[k].size() && v[k][pos] == y){
  68. ans = min(ans, i+k);
  69. }
  70. }
  71. }
  72. printf("%d\n", ans == INF ? -1 : ans);
  73. }
  74. return 0;
  75. }

CodeForces 524C The Art of Dealing with ATM (二分)的更多相关文章

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

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

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

  3. codeforce The Art of Dealing with ATM

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

  4. 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 ...

  5. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  6. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

  7. Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)

    题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...

  8. Codeforces Round #334 (Div. 2) B. More Cowbell 二分

    B. More Cowbell Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/probl ...

  9. Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分

    B. Nearest Fraction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/281/p ...

随机推荐

  1. hdu 2842(矩阵高速幂+递推)

    题意:一个中国环的游戏,规则是一个木棒上有n个环.第一个环是能够任意放上或拆下的,剩下的环x假设想放上或拆下必须前一个环x-1是放上的且前x-2个环所有是拆下的,问n个环最少多少次操作能够所有拆掉. ...

  2. Machine Learning: 一部气势恢宏的人工智能发展史

    转载自:雷锋网 本文作者:陈圳 2016-09-12 09:46 导语:机器学习的从产生,发展,低潮和全盛的历史 雷锋网(公众号:雷锋网)按:本文作者DataCastle数据城堡,主要介绍了机器学习的 ...

  3. SQLMAP源码分析(一)

    说起来,学习Python很大一部分原因是由于对WEB安全的兴趣以及对SQLMAP这款工具的好奇,曾经设想学完Python基础就读一读SQLMAP源码,然而懒病一犯,随之就大江东去.近来,又重新燃起了读 ...

  4. PHP魔术方法之__call与__callStatic方法

    <?php class human{ private function t(){ } //魔术方法__call /* $method 获得方法名 $arg 获得方法的参数集合 */ public ...

  5. 解析java.math.BigInteger类——构造函数

    最早由于做作业,结识了java的BigInrger类.读着读着,越来越觉得有趣.后来作业做完,也不忍丢下它,索性把全部代码研究一遍. 开始的时候,一个上午时间最多读懂2个方法.但是还是有滋有味的坚持了 ...

  6. XML解析PULL

    解析xml是很经常使用的操作,除了SAX和DOM两种最经常使用的解析xml外,Pull解析器解析XML文件. 在Android的源代码中大量的使用Pull解析.pull不仅更加的面相对象,并且使用速度 ...

  7. spring mvc 设置设置默认首页的方式

    背景: 项目使用springmvc管理请求,有一个小的需求,输入域名的时候自动进入某个页面(或者说自动发起某个请求). 过程: 1,首先想到 在web.xml中配置welcome-file-list的 ...

  8. 推荐一个非常好的 IntelliJ IDEA 教程

    教程地址:https://github.com/judasn/IntelliJ-IDEA-Tutorial 作者博客:http://www.youmeek.com/category/software- ...

  9. 在DuiLib中使用MFC类

    比如,想在DuiLib里使用MFC中的CInternetSession 首先,将Project Properties->General->Use of MFC设置为Use MFC in a ...

  10. C++ Lambda表达式和仿函数笔记

    C++11中引入了Lambda表达式,其语法如下: [capture list](parameter list)->return type { function body } 参考博文:C++ ...