codeforces 1015C
1 second
256 megabytes
standard input
standard output
Ivan has nn songs on his phone. The size of the ii-th song is aiai bytes. Ivan also has a flash drive which can hold at most mm bytes in total. Initially, his flash drive is empty.
Ivan wants to copy all nn songs to the flash drive. He can compress the songs. If he compresses the ii-th song, the size of the ii-th song reduces from aiai to bibi bytes (bi<aibi<ai).
Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most mm. He can compress any subset of the songs (not necessarily contiguous).
Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to mm).
If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.
The first line of the input contains two integers nn and mm (1≤n≤105,1≤m≤1091≤n≤105,1≤m≤109) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.
The next nn lines contain two integers each: the ii-th line contains two integers aiai and bibi (1≤ai,bi≤1091≤ai,bi≤109, ai>biai>bi) — the initial size of the ii-th song and the size of the ii-th song after compression.
If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.
- 4 21
10 8
7 4
3 1
5 4
- 2
- 4 16
10 8
7 4
3 1
5 4
- -1
- 题意:给你n件物品,和一个空间为m的背包,这n件物品的初始占用空间为ai,其占用空间可以缩小为bi,但是需要花费ai-bi的花费,求在花费最小的情况下有多少物品不用被压缩
题解:我们换个方向,这n个物品被压缩后的大小为bi,先全部加起来,如果压缩后的物品空间大小小于空间m,那么就可以把所有的物品加入背包,为了使花费最小,
我们剩下的空间必须尽量装满,所以我们就将花费按照从小到大排序,一个个装,装到不能装了后就跳出来就行- 代码如下:
- #include <map>
- #include <set>
- #include <cmath>
- #include <ctime>
- #include <stack>
- #include <queue>
- #include <cstdio>
- #include <cctype>
- #include <bitset>
- #include <string>
- #include <vector>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #define fuck(x) cout<<"["<<x<<"]";
- #define FIN freopen("input.txt","r",stdin);
- #define FOUT freopen("output.txt","w+",stdout);
- //#pragma comment(linker, "/STACK:102400000,102400000")
- using namespace std;
- typedef long long LL;
- typedef pair<int, int> PII;
- const int maxn = 1e5+;
- LL a[maxn];
- LL b[maxn];
- LL c[maxn];
- int main(){
- #ifndef ONLINE_JUDGE
- FIN
- #endif
- LL n,m;
- LL sumb=;
- int flag=;
- scanf("%lld%lld",&n,&m);
- for(int i=;i<n;i++){
- scanf("%lld%lld",&a[i],&b[i]);
- c[i]=a[i]-b[i];
- sumb+=b[i];
- }
- if(sumb>m){
- cout<<"-1"<<endl;
- }else{
- sort(c, c + n);
- int ans = ;
- for (int i = ; i < n; i++) {
- if (sumb + c[i] <= m) {
- sumb += c[i];
- ans++;
- }
- else break;
- }
- printf("%lld\n", n - ans);
- }
- }
codeforces 1015C的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- ruby 数据类型String
一.字符串创建 单引号包含,不支持转义符和内嵌表达式#{}(插值符) str = 'hello world!' 双引号包含 str = "hello world!" 使用%,%Q, ...
- FPGA算法学习(1) -- Cordic(圆周系统之向量模式)
旋转模式用来解决三角函数,实现极坐标到直角坐标的转换,基础理论请参考Cordic算法--圆周系统之旋转模式.那么,向量模式则用来解决反三角函数的问题,体现的应用主要是直角坐标向极坐标转换,即已知一点的 ...
- responsive grid
http://csswizardry.com/csswizardry-grids/ http://unsemantic.com/demo-responsive http://getbootstrap. ...
- [Python 3.X]python练习笔记[2]-----用python实现七段数码管显示年月日
#SevenDigitsDrawV2.py import turtle import time def drawGap(i):#绘制数码管间隔 turtle.penup() turtle.fd(i) ...
- 云计算之路-阿里云上:Web服务器请求到达量突降
今天下午遇到了自使用阿里云以来首次遇到的新情况——http.sys的ArrivalRate突降(说明请求到达IIS的请求数量少了),而且SLB中的3台ECS都出现了这个问题. 1. 10.161.24 ...
- 从webview中加载assets中的html文件
private void readHtmlFormAssets(){ WebSettings webSettings = tipsWebView.getSettings(); webSettings. ...
- 「日常训练」「小专题·图论」 Cow Contest (1-3)
题意 分析 问题是要看出来这是个floyd闭包问题.我没看出来- - 分析之后补充. 代码 // Origin: // Theme: Graph Theory (Basic) // Date: 080 ...
- OpenCV中的按钮问题
在HighGUI中,没有显示提供任何形式的按钮.一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮.开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off.然后通过回调函 ...
- 【WebService】——SOAP、WSDL和UDDI
WebService的三要素:SOAP.WSDL和UDDI.soap用来描述传递信息的格式,wsdl描述如何访问具体的接口,uddi管理.分发查询WebService. 1.SOAP SOAP Sim ...
- Activiti工作流(一)——Activiti Diagram
工作流解决在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务目标,或者促使此目标的实现. 使用Eclipse开发,需要安排工作流插件,详情见下面. Name ...