Sucker to buy cattle

Copy of title

Description

Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget of M units of money (1 <= M <= 10 ^ 14). Cow i costs P_i money (1 <= P_i <= 10 ^ 9), but FJ has K coupons (1 <= K <= N), and when he uses a coupon on cow i, the cow costs C_i instead (1 <= C_i <= P_i) FJ CAN only use One coupon per cow, of Course, the What IS at the maximum number the of cows FJ CAN afford PROBLEM NAME:..? Coupons
FJ ready to buy some new cows, there are N cows on the market (1 <= N <= 50000), the price of the i-th cows Pi (1 <= Pi <= 10 ^ 9). There FJ K coupons, use coupons to buy the i-cow prices dropped Ci (1 <= Ci <= Pi), per cow coupons can only be used once. FJ wondering spend no more than M (1 <= M <= 10 ^ 14) money can buy a maximum number of cows?

Input

 * Line 1: Three space-separated integers: N, K, and M.
 * Lines 2..N+1: Line i+1 contains two integers: P_i and C_i.

Output

 * Line 1: A single integer, the maximum number of cows FJ can afford.

Sample Input

4 1 7
3 2
2 2
8 1
4 3

Sample Output

. 3 
the OUTPUT the DETAILS: The coupon uses FJ. 3 and Buys Cows Cow ON. 1, 2, and. 3, for A + Total cost of. 3. 1 + 2 = 6. The 
sucker regret greedy then three times goose I WA
greedy method should be relatively easy think it's
greedy principles of the coupons by spending runs out
and then we can expect a very interesting approach, according to the price from small to large, pre-k sheets with coupons
but to do so is wrong, because the point when enumerating the current does not consider the impact on the back, just the sort sorted according to price, then if the back of the cow whether to use the coupon price difference is greater than the front (that is, save more money) when the greedy method is wrong, and above this we can easily greedy card out
, then, can we save money in accordance with the number of small to large order to take the first few it? ? ? ? ?
............ greedy with students above the law go right thank you
but we can come up with a conclusion, the low price may be the original price is very low, do not need to use coupons to save
greedy strategy is to go step by step, not with this in mind the impact on the subsequent point, but we can also come up with a sucker DP, former f (i, j) represents the i-th with the j coupon
data and then goose 1e14 forced us to give up
and then we can only find use with regret greed
regret is what does it work? ? ? I do not know is the first all-coupon defeat light used in small cattle above pre-k of the price, and then in the (k + 1, n) intervals on each difference into a small root like a heap
on the code
#include <bits / STDC ++ H.> // smart header 
#define LL Long Long
 #define Fill (A, B) Memset (A, B, the sizeof (A)) // wit converting 
the using  namespace STD;
 const  Long  Long INF = 5e11 + 100 ; 
LL n-, K, m, ANS, CH; 
struct KK { int a, B; BOOL in Flag;} Node [ 50010 ]; // cool definition 
int CMP (KK a, KK B) { return ab & < Bb;}
 int CMP2 (KK a, B KK) { return Aa <of Ba;} // visionary planning
priority_queue<ll,vector<ll >,greater<ll > > Q;
int main(){
    scanf("%lld%lld%lld",&n,&k,&m);
    for (int i=1;i<=n;i++){
        scanf("%lld%lld",&node[i].a,&node[i].b);
        Q.push(node[i].b);
    }//完美的读入 
    sort(node+1,node+1+n,cmp);
    for (ll i=1;i<=k;i++){
        m-=node[i].b;
        if(m> = 0 ) ANS ++ ;
         the else {the printf ( " % D " , ANS); return  0 ;} 
    } Sort (Node + . 1 + K, Node + . 1 + n-, CMP2); // flexible determination 
    / * first coupons run k * /  
    for ( int I = k + . 1 ; I <= n-; I ++ ) {
         IF (! Q.empty ()) CH = INF;
         the else CH = Q.top ();
         IF (+ CH Node [I] .B < Node [I] II.A) { 
            m - Q.top = () + Node [I] .B; Q.pop ();
            Q.push (Node [I] .B - Node [I] II.A); 
        } // alert perceive 
        the else M- Node = [I] II.A; // flexible work 
        IF (m> = 0 ) ANS ++ ;
         the else {the printf ( " % LLD " , ANS); return  0 ;} 
    } the printf ( " % LLD " , ANS); // tactical output 
    return  0 ; // elegant finishing 
}

I did not have to spray someone always comments so I write up a comment

Guess you like

Origin www.cnblogs.com/XYH-xyh/p/11697531.html