Too Much Money
2 seconds
256 megabytes
standard input
standard output
Alfred wants to buy a toy moose that costs c dollars. The store doesn’t give change, so he must give the store exactly c dollars, no more and no less. He has n coins. To make c dollars from his coins, he follows the following algorithm: let S 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 in S 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 from S 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 exactly c. 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 to S and then give up, since adding any other coin would cause the value of the coins in S 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 contains n (1 ≤ n ≤ 200 000) — the number of coins Alfred initially has. Then n lines follow, each containing a single integer x (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).
12
3
5
3
4
5
50
8
1
2
4
8
16
37
37
37
Greed is good
In the first sample, Bob should give Alfred a single coin worth $5. This creates the situation described in the problem statement.
In the second sample, there is no set of coins that will cause Alfred's algorithm to fail.
分析:从小到大枚举答案,然后模拟这个贪心过程,如果不能达到c,则输出答案;
在贪心过程中注意优化,now-=max(1,min(now/(*a),num[*a]))*(*a);
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,c,num[maxn];
set<int>p,tmp;
int main()
{
int i,j;
scanf("%d%d",&c,&n);
rep(i,,n)scanf("%d",&j),p.insert(j),num[j]++;
rep(i,,c)
{
int now=c;
tmp.clear();
p.insert(i);
num[i]++;
while(now>)
{
if(p.empty())return *printf("%d\n",i);
auto a=p.lower_bound(now);
if(a==p.end()||(*a>now&&a!=p.begin()))a--;
tmp.insert(*a);
now-=max(,min(now/(*a),num[*a]))*(*a);
p.erase(*a);
}
if(now<)return *printf("%d\n",i);
for(int x:tmp)p.insert(x);
if(--num[i]==)p.erase(i);
}
puts("Greed is good");
//system("Pause");
return ;
}
随机推荐
- java 导出excel(读数据库案例)
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.S ...
- hdu_5193_Go to movies Ⅱ(带插入删除的逆序对,块状链表)
题目链接:hdu_5193_Go to movies Ⅱ 题意: 有n个人站成一排,每个人的身高为Hi.每次有人加入或者有人离开,就要判断有多少人站反了(i < j&&Hi> ...
- Map获取键值,Map的几种遍历方法
Map 类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个 getKey()方法和一个getValue()方法,Map ...
- Ambari安装组件出错
Caught an exception while executing custom service command: <class 'ambari_agent.AgentException.A ...
- JSP中 JSTL
1,JSTL是JSP的标准标签库的简称,JSP标准标签库包括5类:分别是核心标签库,国际化标签库,SQL标签库,XML标签库,函数标签库: 2,常用的就是核心标签库和SQL标签库: 核心标签库:前置名 ...
- 防暴力破解 Fail2Ban之python
fai2ban的介绍 fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的SSH.SMTP.FTP密码,只要 ...
- java读写串口
http://blog.csdn.net/xxyy888/article/details/8946046
- asp.net URL DES加密 什在URL中的使用
DES 加密 有时会有 + = 这些特殊字符串 Server.UrlDecode(Request["UserName"]) //能解决 = 号,但是 +号会变成空格 HttpUt ...
- 深入体会__cdecl与__stdcall
在学习C++的过程中时常碰到WINAPI或者CALLBACK这样的调用约定,每每觉得十分迷惑.究竟这些东西有什么用?不用他们又会不会有问题?经过在网上的一番搜寻以及自己动手后,整理成以下的学习笔记.1 ...
- jquery 1.9版本后不在支持browser 方法的解决方案
今天对jquery 进行升级,导致项目出错,原来在1.9版本之后 jquery 不支持browser 方法了. 官方建议的又不好用,所以我所jquery 原来的代码摘除来,又扩展回去. //解决jq ...