upc组队赛6 Canonical Coin Systems【完全背包+贪心】
Canonical Coin Systems
题目描述
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;
}
upc组队赛6 Canonical Coin Systems【完全背包+贪心】的更多相关文章
- Canonical Coin Systems【完全背包】
问题 C: Canonical Coin Systems 时间限制: 1 Sec 内存限制: 128 MB 提交: 200 解决: 31 [提交] [状态] [命题人:admin] 题目描述 A ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- 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面值的方案数 ...
- upc组队赛17 Bits Reverse【暴力枚举】
Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...
- upc组队赛3 Chaarshanbegaan at Cafebazaar
Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...
随机推荐
- 《单词的减法》state1~state17(200p)
单词的减法 2016.05.18 state 1 absent accessible accordingly accuracy/accurate acquaint/acquaintance adequ ...
- T1387:搭配购买(buy)
[题目描述] Joe觉得云朵很美,决定去山上的商店买一些云朵.商店里有n朵云,云朵被编号为1,2,…...,n,并且每朵云都有一个价值.但是商店老板跟他说,一些云朵要搭配来买才好,所以买一朵云则与这朵 ...
- 系统安装2---BIOS设置
对于新的电脑通过U盘安装Windows系统,我们第一步绝对是修改BIOS设置.在这里面我们要修改几项比较重要的选项.如下介绍: 修改第一启动项:目的就是让电脑的第一启动项变为U盘启动. 调节引导方式: ...
- java基础知识-基本概念
1.1 java语言有哪些优点? 1.java语言为纯面向对象的语言. 2.平台无关性.java语言的优点便是“一次编译,到处执行”.编译后的程序不会被平台所约束,因此java语言有很好的移植性. 3 ...
- pycharm格式化python代码快捷键Ctrl+Alt+L失效
突然发现按Ctr+Alt+L格式化python失效了,下午时候还好好的.看网上得说法是因为开着的其他软件里用了全局快捷键Ctr+Alt+L,我的是因为被网易云音乐占用了,所以在网易云音乐里把这个快捷键 ...
- css中的文本字间距离、行距、overflow
css字间距.div css字符间距样式实例1.text-indent设置抬头距离css缩进 div设置css样式text-indent : 20px; 缩进了20px 2.letter-spacin ...
- c# 微服务Ocelot网关服务发现
前面提到微服务方案,介绍了该东西,推荐一篇介绍博文https://www.cnblogs.com/jesse2013/p/net-core-apigateway-ocelot-docs.html 我要 ...
- is not an enclosing class
public class A {public class B { }}; 需要实例B类时,按照正逻辑是,A.B ab = new A.B();那么编译器就会出现一个错误–“is not an encl ...
- webpack插件之html-webpack-plugin
官方文档:https://www.npmjs.com/package/html-webpack-plugin html-webpack-plugin 插件专门为由webpack打包后的js提供一个载体 ...
- XMPP即时通讯协议使用(十)——好友关系状态
sub ask recv 订阅 询问 接受 含义 substatus -1- 应该删除这个好友 Indicates that the roster item should be ...