Canonical Coin Systems【完全背包】
问题 C: Canonical Coin Systems
时间限制: 1 Sec 内存限制: 128 MB
提交: 200 解决: 31
[提交] [状态] [命题人:admin]
题目描述
A coin system S is a finite (nonempty) set of distinct positive integers corresponding to coin values, also called denominations, in a real or imagined monetary system. For example, the coin system in common use in Canada is {1, 5, 10, 25, 100, 200}, where 1 corresponds to a 1-cent coin and 200 corresponds to a 200-cent (2-dollar) coin. For any coin system S, we assume that there is an unlimited supply of coins of each denomination, and we also assume that S contains 1,since this guarantees that any positive integer can be written as a sum of (possibly repeated) values in S.
Cashiers all over the world face (and solve) the following problem: For a given coin system and a positive integer amount owed to a customer, what is the smallest number of coins required to dispense exactly that amount? For example, suppose a cashier in Canada owes a customer 83 cents. One possible solution is 25+25+10+10+10+1+1+1, i.e.,8 coins, but this is not optimal, since the cashier could instead dispense 25 + 25 + 25 + 5 + 1 + 1 + 1, i.e., 7 coins (which is optimal in this case). Fortunately, the Canadian coin system has the nice property that the greedy algorithm always yields an optimal solution, as do the coin systems used in most countries. The greedy algorithm involves repeatedly choosing a coin of the
largest denomination that is less than or equal to the amount still owed, until the amount owed reaches zero. A coin system for which the greedy algorithm is always optimal is called canonical.
Your challenge is this: Given a coin system S = {c1, c2, . . . , cn }, determine whether S is canonical or non-canonical. Note that if S is non-canonical then there exists at least one counterexample, i.e., a positive integer x such that the minimum number of coins required to dispense exactly x is less than the number of coins used by the greedy algorithm. An example of a non-canonical coin system is {1, 3, 4}, for which 6 is a counterexample, since the greedy algorithm yields 4 + 1 + 1 (3 coins), but an optimal solution is 3 + 3 (2 coins). A useful fact (due to Dexter Kozen and Shmuel Zaks) is that if S is non-canonical, then the smallest counterexample is less than the sum of the two largest denominations.
输入
Input consists of a single case. The first line contains an integer n (2 ≤ n ≤ 100), the number of denominations in the coin system. The next line contains the n denominations as space-separated integers c1 c2 . . . cn, where c1 = 1 and c1 < c2 < . . . < cn ≤ 106.
输出
Output “canonical” if the coin system is canonical, or “non-canonical” if the coin system is non-canonical.
样例输入
复制样例数据
4 1 2 4 8
样例输出
canonical
题意 : 有n种面额的货币,如果能保证所以金额,用贪心思想算出的货币张数(每次减先大面额的货币)和 正确的货币张数是相同的,就是规范的(输出canonical),如果贪心算出的货币张数比正确算出的多,那就是不规范的(输出non-canonical)
正确的货币张数可以通过完全背包算出 转移方程 dp[i] = d[i - a[j] ] + 1;(dp[i]代表剩余金额为i时已经拥有的张数,a[j]代表第j张钱的面额)
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> P;
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn =2000010;
using namespace std;
int n;
int a[maxn],dp[maxn];
int main(){
sca(n);
for(int i = 0; i < n; i++)
sca(a[i]);
sort(a,a+n);
int maxl = a[n - 1] * 2;
for(int i = 0; i < maxl; i++) dp[i] = INF;
dp[0] = 0;
int flag = 1;
for(int i = 1; i < maxl; i++){
for(int j = 0; j < n; j++){
if(a[j] <= i)
dp[i] = min(dp[i], dp[i - a[j]] + 1); //背包
}
int cnt = 0;
int sum = i;
int pos = n - 1;
while(sum){ //贪心
while(sum >= a[pos]){
sum -= a[pos];
cnt ++;
}
pos--;
}
if(cnt > dp[i]) flag = 0; //不等就是不规范
}
if(flag) printf("canonical\n");
else printf("non-canonical\n");
return 0;
}
Canonical Coin Systems【完全背包】的更多相关文章
- upc组队赛6 Canonical Coin Systems【完全背包+贪心】
Canonical Coin Systems 题目描述 A coin system S is a finite (nonempty) set of distinct positive integers ...
- uva674 Coin Change ——完全背包
link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA-674 Coin Change---完全背包
题目链接: https://vjudge.net/problem/UVA-674 题目大意: 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 思路: 每 ...
- Light oj 1233 - Coin Change (III) (背包优化)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1233 题目就不说明了. 背包的二进制优化,比如10可以表示为1 2 4 3,而 ...
- [luoguP1474] 货币系统 Money Systems(背包)
传送门 背包 ——代码 #include <cstdio> #include <iostream> #define LL long long int v, n; LL f[10 ...
- codeforces 284 E. Coin Troubles(背包+思维)
题目链接:http://codeforces.com/contest/284/problem/E 题意:n种类型的硬币,硬币的面值可能相同,现在要在满足一些限制条件下求出,用这些硬币构成t面值的方案数 ...
- 【题解】coin HDU2884 多重背包
题目 Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 【洛谷】P1474 货币系统 Money Systems(背包dp)
题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1,5,10,20 或 25,50, 和 100的单 ...
- 算法入门经典大赛 Dynamic Programming
111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence ...
随机推荐
- 怎么才能使服务器Nginx(或者Apache)支持字体文件
为了在前端正确地显示字体,浏览器必须使用正确的http header来接受字体文件.如果服务器没有设置要求的头信息,那么有些浏览器就会在控制台报错或者直接不能显示. 可能你的服务器已经配置好了,你无须 ...
- array_rand
array_rand — 从数组中随机取出一个或多个单元 mixed array_rand ( array $array [, int $num = 1 ] ) 从数组中取出一个或多个随机的单元,并返 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- vue的组件基础
组件分为全局组件和局部组件. 组件的核心是template:所有的数据都为template服务. 父组件子组件传值:因为子组件是父组件的个标签,完全等同于添加动态属性: 然后子组件能够通过props: ...
- eclipse maven项目下载jar包失败解决办法
1.找到我们的本地maven仓库目录 我的是 H:\Java\maven\Repository 2.搜索出该目录下的*lastUpdated.properties文件并删除,如下图所示,可以通过模糊搜 ...
- 2、LwIP协议栈规范翻译——协议层
2.协议层 TCP/IP套件中的协议是以层次的方式设计的,其中每个协议层解决了通信问题的单独部分.这种分层可以用作设计协议实现的指南,因为每个协议可以与另一个分开实现.然而,以严格分层的方式实现协议可 ...
- 对象的创建与OOP-Klass模型
1.JVM中OOP-KLASS模型 在JVM中,使用了OOP-KLASS模型来表示java对象,即:1.jvm在加载class时,会创建instanceKlass,表示其元数据,包括常量池.字段.方法 ...
- Navicat 用ssh通道连接时总是报错 (报错信息:SSH:expected key exchange group packet form serve
转:https://blog.csdn.net/qq_27463323/article/details/76830731 之前下了一个Navicat 11.0 版本 用ssh通道连接时总是报错 (报错 ...
- iOS代理模式(delegate)的使用
前言: 代理模式是iOS中非常重要的一个模式,iOS SDK中的系统控件几乎都用到了代理模式.代理模式用来处理事件监听.参数传递功能. 协议创建(Protocol): 可手打如下代码,或者在代码块里面 ...
- 家庭记账本之微信小程序(五)
wxml的学习 WXML(WeiXin Markup Language)是框架设计的一套标签语言,结合基础组件.事件系统,可以构建出页面的结构. 用以下一些简单的例子来看看WXML具有什么能力: 数据 ...