Canada Cup 2016 E. Too Much Money(贪心)
来源:互联网 发布:博采网络推广怎么做 编辑:程序博客网 时间:2024/06/04 19:02
Alfred wants to buy a toy moose that costs c dollars. The store doesn’t give change, so he must give the store exactlyc dollars, no more and no less. He has n coins. To make c dollars from his coins, he follows the following algorithm: letS be the set of coins being used. S is initially empty. Alfred repeatedly adds to S the highest-valued coin he has such that the total value of the coins inS after adding the coin doesn’t exceed c. If there is no such coin, and the value of the coins in S is still less than c, he gives up and goes home. Note that Alfred never removes a coin fromS after adding it.
As a programmer, you might be aware that Alfred’s algorithm can fail even when there is a set of coins with value exactlyc. For example, if Alfred has one coin worth $3, one coin worth $4, and two coins worth $5, and the moose costs $12, then Alfred will add both of the $5 coins toS and then give up, since adding any other coin would cause the value of the coins inS to exceed $12. Of course, Alfred could instead combine one $3 coin, one $4 coin, and one $5 coin to reach the total.
Bob tried to convince Alfred that his algorithm was flawed, but Alfred didn’t believe him. Now Bob wants to give Alfred some coins (in addition to those that Alfred already has) such that Alfred’s algorithm fails. Bob can give Alfred any number of coins of any denomination (subject to the constraint that each coin must be worth a positive integer number of dollars). There can be multiple coins of a single denomination. He would like to minimize the total value of the coins he gives Alfred. Please find this minimum value. If there is no solution, print "Greed is good". You can assume that the answer, if it exists, is positive. In other words, Alfred's algorithm will work if Bob doesn't give him any coins.
The first line contains c (1 ≤ c ≤ 200 000) — the price Alfred wants to pay. The second line containsn (1 ≤ n ≤ 200 000) — the number of coins Alfred initially has. Thenn lines follow, each containing a single integerx (1 ≤ x ≤ c) representing the value of one of Alfred's coins.
If there is a solution, print the minimum possible total value of the coins in a solution. Otherwise, print "Greed is good" (without quotes).
123534
5
508124816373737
Greed is good
题意:一堆硬币每次贪心取最大问能否凑成C,现在已经保证了当前的n个硬币可以凑出c,问最少添加价值多少的硬币使得凑不出C。
分析:首先,不管添加多少硬币都可以转化为添加一枚的情况,然后我们就可以暴力枚举它的面额,然后每次最坏√n的判断。
#include <cstdio>#include <iostream>#include <map>using namespace std;map <int,int> f;int n,c,x;bool deal(int x){int temp = x;while(x){auto p = f.upper_bound(temp);if(p == f.begin()) return false;p--;x -= min((x / p->first)*(p->first),p->first * (p->second));temp = min(x,p->first-1);}return true;}int main(){scanf("%d",&c);scanf("%d",&n);for(int i = 1;i <= n;i++) {scanf("%d",&x);f[x]++;}for(int i = 1;i < c;i++){f[i]++;if(!deal(c)){printf("%d",i);return 0;}if(f[i] == 1) f.erase(i);else f[i]--;}printf("Greed is good");return 0;}
- Canada Cup 2016 E. Too Much Money(贪心)
- 枚举,模拟(Too Much Money,cf Canada Cup 2016 E)
- Canada Cup 2016 E
- [CF725E]Too Much Money
- Codeforces 725E Too much money (暴力模拟)
- Canada Cup 2016 A,B,C,D,E
- Canada Cup 2016 D. Contest Balloons (贪心 + 优先队列)
- Canada Cup 2016 D. Contest Balloons(贪心,优先队列)
- [堆 贪心] Canada Cup 2016 D. Contest Balloons
- Canada Cup 2016 D. Contest Balloons 小根堆+贪心
- Canada Cup 2016 F. Family Photos(贪心,想法,好题)
- Canada Cup 2016
- Canada Cup 2016
- Canada Cup 2016 C 模拟
- Canada Cup 2016 D. Contest Balloons详解(贪心+优先队列)
- Canada Cup 2016 C. Hidden Word .
- Canada Cup 2016 A - Jumping Ball
- Canada Cup 2016 D. Contest Balloons
- Windows不能在本地计算机启动IIS Admin Service
- 设计模式之模板方法模式
- MySQL数据库学习一
- java使用sax解析xml
- 0MQ绑定Delphi版-说明
- Canada Cup 2016 E. Too Much Money(贪心)
- java中使用dom解析xml
- ADO.NET操作数据库(二)
- jdk环境变量配置
- java中使用dom4j解析xml
- spacemacs email layer
- javac,wsimport不是内部或者外部命令的解决方法win7系统
- session保存的是值还是引用?
- java学习推荐书籍