Description

有一个国家,流通着N种面值的硬币,其中包括了1分硬币。另外,有一种面值为K分的纸币,它超过了所有硬币的面值。 有一位硬币收藏家,他想收集每一种面值的硬币样本。他家里已经有一些硬币,但是现在他只带着一张K分纸币去商店。 商店里总共有K-1种商品,价格分别为1分、2分……K-1分。这家商店使用以下算法找零: 1、 假设总共需要找A分; 2、 寻找最高的不超过A的硬币面值,设它为B分硬币; 3、 给顾客一枚B分硬币,然后令A为A-B; 4、 如果A=0,算法结束;否则转2。 收藏家想用他的K分纸币买一件商品。请你编写程序,计算:  收藏家能够得到多少种他还没有过的硬币?  在满足上一问的前提下,他能够买的最贵的商品是什么?

Input

第一行为两个整数N(1≤N≤500000)和K(2≤K≤1000000000)。 以下N行描述流通的硬币。第i+1行包含两个整数ci(1≤ci

Output

第一行为一个整数,表示收藏家最多能获得多少种之前还没有的硬币。 第二行为一个整数,表示在前一问的前提下,收藏家能购买的最贵的商品价格。
设f[i]为买价格k-i的商品可得到的不同的之前还没有的硬币数,则f[0]=0,f[i]=f[i%a]+b,其中i>0,a为面值不超过i的最大面值硬币,b为其是否之前还没有
观察f[i]的性质,可知f[i]的前缀最大值有O(n)次突变,且每次增加1,因此可以维护这些突变点,最后一个突变点就对应了答案
从小到大枚举硬币面值,更新时只需考虑上一个突变点与当前硬币能否得到一个新的突变点
#include<cstdio>
#include<algorithm>int _(){
int x=,c=getchar();
while(c<)c=getchar();
while(c>)x=x*+c-,c=getchar();
return x;
}
struct item{
int x,d;
}v[];
bool operator<(const item&a,const item&b){
return a.x<b.x;
}
int n,k,ps[],pp=;
int main(){
n=_();k=_();
for(int i=;i<n;++i)v[i].x=_(),v[i].d=!_();
std::sort(v,v+n);
v[n].x=k;
for(int i=,a;i<n;++i)if(v[i].d){
a=v[i].x+ps[pp];
if(a<v[i+].x)ps[++pp]=a;
}
if(!pp)ps[pp]=;
printf("%d\n%d\n",pp,k-ps[pp]);
return ;
}

bzoj1346: [Baltic2006]Coin的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  4. [luogu2964][USACO09NOV][硬币的游戏A Coin Game] (博弈+动态规划)

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  5. LeetCode Coin Change

    原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...

  6. ACM Coin Test

    Coin Test 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 As is known to all,if you throw a coin up and let ...

  7. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. leetcode:Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  9. UVa 674 Coin Change【记忆化搜索】

    题意:给出1,5,10,25,50五种硬币,再给出n,问有多少种不同的方案能够凑齐n 自己写的时候写出来方案数老是更少(用的一维的) 后来搜题解发现,要用二维的来写 http://blog.csdn. ...

随机推荐

  1. github for windows 安装失败解决方案(亲测)

    早之前就有接触github,也在公司机子上装过,一路下来挺顺畅的.夏老师还纳闷他的机子装不上,我说,有鬼! 然而时隔一个月自己再来装,却在自己的本本上遇到鬼了. 然而网上论坛收了一堆,各种试.果断放弃 ...

  2. Windows下Nginx+Tomcat整合的安装与配置

    原帖:http://zyjustin9.iteye.com/blog/2017394 相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人 ...

  3. Python 结巴分词

    今天的任务是对txt文本进行分词,有幸了解到"结巴"中文分词,其愿景是做最好的Python中文分词组件.有兴趣的朋友请点这里. jieba支持三种分词模式: *精确模式,试图将句子 ...

  4. tyvj1013 - 找啊找啊找GF ——二维背包变种

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1013 好吧,这题没节操=_= 状态f[u,v,i]表示:消费u的人民币和v的人品同时泡到i个mm所需要的 ...

  5. zookeeper的 目录加密

    import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zo ...

  6. 课堂所讲整理:HTML--8Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunmen ...

  7. nginx配置说明

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...

  8. (转) An overview of gradient descent optimization algorithms

    An overview of gradient descent optimization algorithms Table of contents: Gradient descent variants ...

  9. mysql启动与关闭(手动与自动)

    手动管理mysql的启动与关闭 [root@mysql ~]# service mysql start --手动启动mysqlStarting MySQL. SUCCESS![root@mysql ~ ...

  10. Android Studio导入Project的方法

    Android Studio到现在已经发展到0.8+的版本了,最近也在试着使用它,原因是多方面的,一个毕竟是未来的趋势,二则是github上越来越多的大牛开源项目都是基于Android Studio的 ...