USACO[CowCoupons]

USACO[CowCoupons]

This title is a great problem of greed, the only drawback is that the data too much water, strongly called for the strengthening of data. (Of course, I know no one here to call me a bird ...)
The first question I believe some people are thinking by after sorting preferential prices, can only buy, but this is obviously wrong.
For example, you have two cows and a coupon, the price of two cows before and after the preferential concessions were \ ({50, 25} \) and \ (10000,1} {\) .
this should obviously.

Then we can consider what to cattle with coupons is the most value. One idea is to press the above approach taken \ (k \) cow, and then to the rest of the cattle one by one to consider whether to use coupons better on the cow.
a very simple idea is back, but this is clearly not OK is the complexity of the heaven ... a pair of series
that we consider another cow coupon better condition compared to how a cow a.
not difficult to find, if for two cows \ (i, J \) , there \ [c_i + p_j <c_j + p_i \] then obviously use the coupon in the \ (i \) the cow would be more excellent.
so me remove the item and found this thing became this \ [P_i - C_i> p_j - C_J \] , so we put before each of the first coupon of Imperial cow this thing pressed into the small root pile, one by one consideration behind cattle can be.
but when I was with the judge's conclusion the beginning. It should be noted that, in a few head of cattle after considering the time, according to the original price row over order, because without a coupon, and you also money, apparently so buy a comparative advantage.
\ (Code: \)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define int long long
#define pii pair < int , int >
#define X first
#define Y second

using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;

const int N = 5e4 + 100 ;

priority_queue < pii , vector < pii > , std::greater < pii > > q ;
int n , k , m , tot , ans ;
pii cow[N] ;

inline bool cmp (pii a , pii b) {
    if ( a.Y != b.Y ) return a.Y < b.Y ;
    return a.X < b.X ;
}

signed main () {
    scanf ("%lld%lld%lld" , & n , & k , & m ) ; ans = tot = 0 ;
    rep ( i , 1 , n ) scanf ("%lld%lld" , & cow[i].Y , & cow[i].X ) ; // P C
    std::sort ( cow + 1 , cow + n + 1 ) ;
    rep ( i , 1 , k ) {
        tot += cow[i].X ; ++ ans ;
        if ( tot > m ) { printf ("%lld\n" , i - 1 ) ; return 0 ; }
        q.push ( { cow[i].Y - cow[i].X , i } ) ;
    }
    ans = k ; std::sort ( cow + k + 1 , cow + n + 1 , cmp ) ;
    rep ( i , k + 1 , n ) {
        pii tmp = q.top () ;
        if ( cow[tmp.Y].X + cow[i].Y > cow[tmp.Y].Y + cow[i].X ) {
            ++ ans ; tot -= cow[tmp.Y].X ;
            tot += ( cow[i].X + cow[tmp.Y].Y ) ;
            q.pop () ; q.push ( { cow[i].Y - cow[i].X , i } ) ;
        } else { ++ ans ; tot += cow[i].Y ; }
        if ( tot > m ) { printf ("%lld\n" , ans - 1 ) ; return 0 ; }
    }
    printf ("%lld\n" , n ) ;
    system ("pause") ; return 0 ;
}

Guess you like

Origin www.cnblogs.com/Equinox-Flower/p/11402156.html