[Codeforces] 1015C. Songs Compression

題目URL: http://codeforces.com/contest/1015/problem/C
相減sort後,aa總和從小減到大一邊記數,比b小就輸出


#include<bits/stdc++.h>
using namespace std;
#define int  long long
int aa,bb,str[100005];
signed main()
{
    int a,b,total=0,totala=0,tot=0;
    cin >> a >> b;
    for(int i=0;i<a;i++)
    {
        cin >> aa >> bb;
        str[i]=aa-bb;
        total+=bb;
        totala+=aa;
    }
    sort(str,str+a);
    if(total>b)
        cout << -1 << endl;
    else
    {
        for(int i=a-1;i>=0;i--)
        {
            //cout << str[i] << endl;
            if(totala<=b)
                break;
            totala-=str[i];
            tot++;
        }
        cout << tot << endl;
    }
    return 0;
}

留言

這個網誌中的熱門文章

[ZJ] b229: TOI 2009 第一題:路徑問題

交大資工(APCS組)(面試&心得)

滿是挫傷的ION CAMP