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. Python3爬虫(八) 数据存储之TXT、JSON、CSV

    Infi-chu: http://www.cnblogs.com/Infi-chu/ TXT文本存储 TXT文本存储,方便,简单,几乎适用于任何平台.但是不利于检索. 1.举例: 使用requests ...

  2. P3388 【模板】割点

    题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,m 下面m行每行输入x,y表示x到y有一条边 输出格式: 第一行输出割点个数 第二行按照 ...

  3. python2.7入门---字典(Dictionary)

        这次咱们记录的是python中的字典这个鬼,首先我们得了解,字典是另一种可变容器模型,且可存储任意类型对象.字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 ...

  4. JSON初体验(二):Gson解析

    今天,我们来介绍一下Gson的jar包的用法. JSON解析之Gson 特点:编码简介,谷歌官方推荐 数据之间的转换: 1.将json格式的字符串{}转换成为java对象 API: <T> ...

  5. 公用的cefsharp窗口

    书接上回,.net实现一个nw,一个字,简单. 结构,无废话,上图. 要说这部分上回展示过的,大致结构如此,其实要说清楚结构,还是得从工作流程开始说起 流程 1.通过桌面的快捷方式启动WebOnDes ...

  6. python自动化之BDD框架之lettuce初识问题集

    最近在学习虫师老师编写的python自动化的书.其中讲到了BDD结构lettuce入门一章. 因为是小白,按部就班地进行操作,先不谈执行操作如何,先来讲讲遇到的几个坑,和怎么解决的: 第一坑:pyth ...

  7. Bellman_ford标准算法

    Bellman_ford求最短路可以说这个算法在某些地方和dijkstra还是有些相似的,它们的松弛操作基本还是一样的只不过dijkstra以图中每个点为松弛点对其相连接的所有边进行松弛操作 而Bel ...

  8. SPOJ 375 Query on a tree(树链剖分)(QTREE)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  9. 简明Python3教程 1.介绍

    Python是少有的几种既强大又简单的编程语言.你将惊喜地发现通过使用Python即可轻松专注于解决问题而非和你所用的语言格式与结构. 下面是Python的官方介绍: Python is an eas ...

  10. servlet入门(1)

    第一个servlet类 1.编写一个java类,继承HttpServlet类 2.重写doget和dopost方法 3.Servlet程序在tomcat服务器运行 第一步:找到server窗口,并新建 ...