[Usaco12feb] Bull coupon Cow coupons (heap, greed)

Source: Internet
Author: User

[Usaco12feb] Bull coupon Cow coupons (heap, greedy) title description

Farmer John needs new cows! There is n cows for sale (1 <= N <= 50,000), and FJ have to spend no more than he budget of M units of money (1 < ; = M <= 10^14). Cow i costs p_i money (1 <= p_i <= 10^9), but FJ had 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.

What is the maximum number of cows FJ can afford?

FJ ready to buy some new cows, the market has N cows (1<=n<=50000), the first cow price is Pi (1<=pi<=10^9). FJ has k coupons, the price will be reduced to Ci (1<=CI<=PI) when purchasing the first cow with coupon, each cow can only use one coupon. FJ want to know how many cows can be bought with no more than M (1<=m<=10^14) money?

Input/output format

Input format:

    • Line 1:three space-separated integers:n, K, and M.

    • Lines 2..n+1:line i+1 contains, integers:p_i and C_i.

Output format:

    • Line 1: A single integer, the maximum number of cows FJ can afford.
Input/Output sample

Input Sample # #:

4 1 7
3 2
2 2
8 1
4 3

Sample # # of output:

3

Description

FJ has 4 cows, 1 coupon, and a budget of 7.

FJ uses the coupon on Cow 3 and buys cows 1, 2, and 3, for a total cost of 3 + 2 + 1 = 6.

Heap analog back-up operation

It is easy to see that direct greed is wrong because we have a limit on the total amount of money.
So can we get the right answer by adjusting the fake greedy strategy?
first greedy to take the smallest k preferential price, and then consider how to regret.
for both bull \ (i,j\) , suppose \ (i\) used coupons, \ (j\) is not used, under what circumstances will \ (j\) with coupon \ (i\) not better? Very simple: \ (c[i]+p[j]>p[i]+c[j]\) . Use a heap to maintain a coupon for the cattle, making a heap to maintain the cattle that have not yet been selected. Every time the choice of cattle is not selected to buy the original price or "regret."
But there is one question that you can break off every time you have enough money? It's not a good feeling to think carefully. Want to be able to answer

#include <bits/stdc++.h> #define LLL Long longusing namespace Std;lll read () {lll X=0,w=1;char Ch=getchar (); while (ch> ' 9 ' | |    ch< ' 0 ') {if (ch== '-') W=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x*w;} Const LLL n=50010;lll n,k,m,ans;bool vis[n];struct node{lll p,c,v;} F[n];p riority_queue< pair<int,int> >q1,q2;bool cmp (node P,node q) {return p.c<q.c;}    int main () {n=read (); K=read (); M=read ();    for (lll i=1;i<=n;i++) F[i].p=read (), F[i].c=read (), f[i].v=f[i].p-f[i].c;    Sort (f+1,f+1+n,cmp);        for (lll i=1;i<=k;i++) {if (m<f[i].c) {Cout<<i-1;return 0;}    M-=f[i].c;q1.push (Make_pair (f[i].v,i));    }ans=k;    for (lll i=k+1;i<=n;i++) Q2.push (Make_pair (-f[i].p,i));        while (m&&ans!=n) {lll i=q2.top (). Second;q2.pop ();        LLL j=q1.top (). Second;q1.pop ();            if (F[J].C+F[I].P&GT;F[I].C+F[J].P) {if (m<f[j].v+f[i].c) break; Q1.push(Make_pair (f[i].v,i));        m-=f[j].v+f[i].c;ans++;            } else {if (M&LT;F[I].P) break;            Q1.push (Make_pair (f[j].v,j));        m-=f[i].p;ans++; }}cout<<ans<<endl;}

[Usaco12feb] Bull coupon Cow coupons (heap, greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.