C. Songs Compression
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input

Copy
4 21
10 8
7 4
3 1
5 4
output

Copy
2
input

Copy
4 16
10 8
7 4
3 1
5 4
output

Copy
-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的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. Mongoose模式的扩展

    模式的扩展 默认值 默认值的类型: 固定值.即使生成 代码展示: var mongoose = require('mongoose');mongoose.connect('mongodb://loca ...

  2. go执行外部应用

    go执行外部应用 最近想将原来用asp.net mvc写的一个公司内部网站改用beego来写,但发现其中有一个功能是,能将加密的sqlite文件进行解密,因为这个解密是不能公开的,但有些同事需要查看这 ...

  3. Create Fiori List App Report with ABAP CDS view – PART 1

    From Create Fiori List App Report with ABAP CDS view – PART 1 In this blog, I am going to show How C ...

  4. EAS_Table

    SHR人力 员工表 T_BD_PERSON fbirthday 出生日期 femployeetypeid       员工状态   员工状态  T_HR_BDEMPLOYEETYPE        T ...

  5. android 怎么判断activity 从哪里启动的

    有时候,你想要知道,有一个activity 从哪里启动的.怎么才能知道呢? 1.前提是,androidstadio 你下载了源码.找到你的activityBase的实现类,在startActivity ...

  6. MyEclipse - 问题集 - 创建Maven项目,JDK版本默认是1.5

    修改Maven的配置文件settings.xml,增加profile节点,如下所示: <profile> <id>jdk-1.8</id> <activati ...

  7. java网络编程框架

    虽然写过一些网络编程方面的东西,但还没有深入研究过这方面的内容,直接摘录一些文章,后续整理 原文地址:http://blog.csdn.net/lwuit/article/details/730613 ...

  8. kafka常用命令笔记

    0.查看有哪些主题: ./kafka-topics.sh --list --zookeeper 192.168.0.201:12181 1.查看topic的详细信息 ./kafka-topics.sh ...

  9. OpenCV实现SIFT图像拼接源代码

    OpenCV实现SIFT和KDtree和RANSAC图像拼接源代码,此源代码由Opencv2.4.13.6和VC++实现,代码本人已经调试过,完美运行,效果如附图.Opencv2.4.13.6下载地址 ...

  10. sqlserver 找出字符第N次出现的位置

    [1编写函数]CREATE FUNCTION IndexOf(@str VARCHAR(500),@value VARCHAR(50),@posIndex INT)RETURNS int AS BEG ...