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 ...
随机推荐
- swust oj 1075
求最小生成树(Prim算法) 1000(ms) 10000(kb) 2251 / 4487 Tags: 生成树 求出给定无向带权图的最小生成树.图的定点为字符型,权值为不超过100 的整形.在提示中已 ...
- 巧用border效果
目的: 我们在做css的时候为了提高网站的效率减少服务器请求,我们可以通过css来实现一些简单的图片特效,比如说三角形,这篇文章讲解的是通过边框实现不同的效果. 上面样式部分代码: <style ...
- js运用4
---恢复内容开始--- 1.函数 关键字function 复习 var 是js的关键字,用于声明变量,声明在内存模块完成,定义(=)是在执行模块完成. var可以在内存模块提前(js代码执行 ...
- Python学习之旅(二十九)
Python基础知识(28):常用第三方模块 一.Pillow PIL(Python Imaging Library):提供了强大的图像操作功能,可以通过简单的代码完成复杂的图像处理,是Python平 ...
- 手写AVL 树(上)
平衡二叉树 左旋,右旋,左右旋,右左旋 具体原理就不说了,网上教程很多.这里只实现了建树的过程,没有实现删除节点的操作. 下一篇会实现删除节点的操作. // // main.cpp // AVL // ...
- 安装php后无法动态加载库
安装Apache.mysql.PHP并配置完成后使用phpinfo测试显示正常,但是无法动态增加库 原因:安装PHP后不会生成php.ini文件,但是phpinfo测试正常 解决方法: 1.查看配置文 ...
- C#4.5-4.7学习总结
4.5讲的是this关键字.它用于表示对当前实例的引用,它有三种用法,一是访问属性,解决与局部变量名称冲突问题,二是访问成员方法,在类中调用自己的成员方法,就是在一个方法中,通过this.方法名,调用 ...
- AsyncHttpClient使用
github地址:AsyncHttpClient, API:API 1.X和2.X差别很大,我用的1.X中的最新版 1.9.39. 这是一个异步请求的工具,越简单越好,不喜欢再结合netty使用.As ...
- Linux 安装python3.7.0
我这里使用的时centos7-mini,centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同,可通过 python --V 或 python --version 查看系统自 ...
- 更多more 123123循环