题目描述

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

题解:

给你一些货币的价值,假设给出一个价值,让你用已有的货币价值去组合成给出的价值,有两种策略,一种是每次都选最大的,最大的选不了再选次大的,直到凑齐;另一种是选择出组合出给出价值所需的最小货币数,问,是否存在这样一个价值,能让这两种策略选出的货币数量不相等,如果存在就是不规范的,不存在就是规范的。题目有一个提示,最小的反例小于两个最大面额的总和。所以我们就从1枚举到最大面额的两倍,先暴力求出第一种策略每个价值所需的数量,然后再用背包求出每个价值的最小货币表示数。然后一一比对,存在一个不相等就是不规范的了,训练赛的时候,一直WA,比赛结束的才注意到我们数组开小了Orz,希望以后不要在这么粗心啦。

AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
#pragma warning(disable:4996)
using namespace std;
typedef long long ll;
const int maxn = 2000005;
const int INF = 0x3f3f3f3f;
int n, m;
int a[maxn];
int dp[maxn];
int num[maxn];
int main() {scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}memset(dp, INF, sizeof(dp));dp[0] = 0;int sum = maxn;for (int i = 1; i <= maxn; i++) {int temp = i;int pos = n - 1;while(temp){if(temp >= a[pos]){num[i] += temp/a[pos];temp %= a[pos];}pos--;} }for (int i = 0; i < maxn; i++) {for (int j = 0; j < n; j++) {if (i - a[j] >= 0) {dp[i] = min(dp[i], dp[i - a[j]] + 1);}}}bool flag = false;for (int i = 1;i<a[n-1] * 2; i++) {if (dp[i] != num[i]) {printf("non-canonical\n");flag = true;break;}}if (!flag) {printf("canonical\n");}
}

Canonical Coin Systems相关推荐

  1. Font Configuration and Customization for Open Source Systems - 白皮书翻译和深入

    font config 作为一个linux系统的font的配置的基本要素,如何使用的一份手册翻译: 本文尝试从font config手册出发,介绍font config的基本概念和基本元素,以期待在了 ...

  2. JPM Coin三部曲 (上) :深入理解摩根幣的運作

    "全美最大的銀行摩根大通推出首個由美國銀行支持的加密貨幣".有人說這是主流金融機構開始接納加密貨幣的標誌,有人調侃摩根 CEO 傑米·戴蒙一邊炮轟比特幣.一邊忍不住自己發幣,還有密 ...

  3. infoq_InfoQ与Azul Systems Gil Tene谈论Zing,Zulu和新版本

    infoq .marker { color: #000; background-color: #FF0; } Azul Systems在生产超过大型系统行业标准的JVM和算法方面一直处于行业领先地位. ...

  4. Serial Programming Guide for POSIX Operating Systems

    Serial Programming Guide  for  POSIX Operating Systems POSIX操作系统串行编程指南 5th Edition, 3rd Revision  Co ...

  5. Serial Programming Guide for POSIX Operating Systems(转)

    Serial Programming Guide for POSIX Operating Systems POSIX操作系统串行编程指南 5th Edition, 3rd Revision Copyr ...

  6. Asymmetric numeral systems (ANS)非对称数字系统最全资料整理

    最近在看Asymmetric numeral systems (ANS),一开始能找到的资料非常少,后面不断的从各个渠道找到一些参考材料,在这里整理一下: 论文 Jarek Duda的几篇: Asym ...

  7. 【数据库】数据库管理系统(Database Management Systems)

    数据库管理系统(Database Management Systems) 数据库管理系统是一种操纵和管理数据库的大型软件,用于建立.使用和维护数据库,简称 DBMS.它对数据库进行统一的管理和控制,以 ...

  8. JPM Coin 三部曲 (上) - 深入理解摩根币的运作

    JPM Coin 三部曲 (上) - 深入理解摩根币的运作 2019-04-17 潘超 作者系 MakerDAO 中国区负责人 "全美最大的银行摩根大通推出首个由美国银行支持的加密货币&qu ...

  9. Distributed systems theory for the distributed systems engineer 翻译 中英对照

    Distributed systems theory for the distributed systems engineer 适合 分布式系统工程师 的 分布式系统理论 Gwen Shapira, ...

  10. NVIDIA Nsight Systems CUDA 跟踪

    NVIDIA Nsight Systems CUDA 跟踪 CUDA跟踪 NVIDIA Nsight Systems能够捕获有关在概要过程中执行CUDA的信息. 可以在报告的时间轴上收集和呈现以下信息 ...

最新文章

  1. UITableView数据的添加、删除、移动
  2. mac OS X中升级php5.5至php5.6 or php7
  3. 长方形与圆最近连线LISP_餐桌到底选方还是圆?可千万别买错了,今天我们好好聊聊...
  4. java爬去赶集,爬取赶集网二手物品下所有物品的信息
  5. jsp自定义标签详解(2)
  6. Pytorch:优化器
  7. 【解决方案 二】---设置mysql5.7编码集为utf8mb4
  8. php截取字符串utf8,php自定义截取中文字符串-utf8版
  9. u盘写保护怎么才能真正去掉
  10. idea 怎么快速创建类的快捷键_Idea 常用快捷键整理
  11. Flask Web——Jinjia2模板的使用
  12. android 字幕跑马灯,led跑马灯字幕
  13. java实现第六届蓝桥杯分机号
  14. V-SLAM重读(3):SVO代码阅读和调试修改
  15. Acer S3 拆机换固态硬盘!【我的Acer S3小三,时尚时尚最时尚!】
  16. android手机传输,智能手机怎么传文件 安卓手机怎么传文件 安卓手机文件传输方法集合...
  17. 软件测试以bug数来考核,软件测试能力提升及其思考
  18. 欧几里德关系的S5---刘易斯逻辑之十一
  19. 史蒂夫·乔布斯-读书笔记3
  20. QCC51XX---Kymera调节音量

热门文章

  1. 数字人事系统 java_市国税局“数字人事”信息系统正式上线
  2. 结合可变形注意力的视觉Transformer
  3. 动态圣诞树html,圣诞了,送大家一颗HTML5圣诞树
  4. 仓库规模操作系统的背景之操作系统
  5. 【Java】异步回调转为同步返回
  6. 香橙派 Ubuntu修改系统时间
  7. Win10+1050Ti配置Tensorflow教程
  8. Python求向量的余弦值
  9. 我国三大常用坐标系:北京54、西安80和WGS-84
  10. 人员离职it检查_员工离职的IT流程