CodeForces C. Songs Compression

http://codeforces.com/contest/1015/problem/C

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 2110 87 43 15 4

output

Copy

2

input

Copy

4 1610 87 43 15 4

output

Copy

-1

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int N, M;
long long sum1 = 0, sum2 = 0;

struct Node{
    int s;
    int e;
    int t;
}node[maxn];

bool cmp(const Node& a, const Node& b) {
    return a.t > b.t;
}

int main() {
    scanf("%d%d", &N, &M);
    for(int i = 1; i <= N; i ++) {
        scanf("%d%d", &node[i].s, &node[i].e);
        sum1 += node[i].e;
        sum2 += node[i].s;
        node[i].t = node[i].s - node[i].e;
    }

    if(sum1 > M) {
        printf("-1\n");
        return 0;
    }
    if(sum2 <= M) {
        printf("0\n");
        return 0;
    }

    int ans = 0;
    sort(node + 1, node + 1 + N, cmp);
    for(int i = 1; i <= N; i ++) {
        sum2 -= node[i].t;
        ans ++;
        if(sum2 <= M)
            break;
    }
    printf("%d\n", ans);
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9845813.html

时间: 2025-01-15 11:20:35

CodeForces C. Songs Compression的相关文章

Codeforces 650C Table Compression (并查集)

题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <algorithm> 5 #include <cmath> 6 #include <cstdlib> 7 #include <bits/stdc

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w

Educational Codeforces Round 25 F. String Compression(kmp+dp)

题目链接:Educational Codeforces Round 25 F. String Compression 题意: 给你一个字符串,让你压缩,问压缩后最小的长度是多少. 压缩的形式为x(...)x(...)  x表示(...)这个出现的次数. 题解: 考虑dp[i]表示前i个字符压缩后的最小长度. 转移方程解释看代码,这里要用到kmp来找最小的循环节. 当然还有一种找循环节的方式就是预处理lcp,然后通过枚举循环节的方式. 这里我用的kmp找的循环节.复杂度严格n2. 1 #inclu

Codeforces Gym 100463B Music Mess Hash 逻辑题

Music Mess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description Francis really likes his music. He especially likes that song ’Zombie Nation’. Or was that the album? Or maybe the album was ’Kernkraft 4

CodeForces Dubstep 题解

Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them. Let's assume that a song consists of some number of word

CodeForces 569A 第六周比赛C踢

C - C Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 569A Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha lis

CodeForces 208A

A - WUB(*?-?*)WUB Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 208A Description Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance.

codeforces Name That Tune (概率dp)

题意: D - Name That Tune Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 499D Appoint description:  System Crawler  (2015-01-05) Description It turns out that you are a great fan of rock b

Educational Codeforces Round 59 (Rated for Div. 2) DE题解

Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contest/1107/problem/D 题意: 给出一个n*(n/4)的矩阵,这个矩阵原本是一些01矩阵,但是现在四个四个储存进二进制里面,现在给出的矩阵为0~9以及A~F,表示0~15. 然后问这个矩阵能否压缩为一个(n/x)*(n/x)的矩阵,满足原矩阵中大小为x*x的子矩阵所有数都相等(所有子矩阵构