【bzoj2590】[Usaco2012 Feb]Cow Coupons

来源:互联网 发布:linux 批量删除进程 编辑:程序博客网 时间:2024/06/06 03:27

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. What is the maximum number of cows FJ can afford? PROBLEM NAME: coupons

FJ准备买一些新奶牛,市场上有N头奶牛(1<=N<=50000),第i头奶牛价格为Pi(1<=Pi<=10^9)。FJ有K张优惠券,使用优惠券购买第i头奶牛时价格会降为Ci(1<=Ci<=Pi),每头奶牛只能使用一次优惠券。FJ想知道花不超过M(1<=M<=10^14)的钱最多可以买多少奶牛?

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

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

题解
先买c最小的k个
剩下的维护p最小与c最小两个小根堆
选择c最小的加已经买的差价最小的或者p最小的
那个更小选择哪种

代码

#include<bits/stdc++.h>#define pa pair<int,int>#define N 500005#define ll long long#define inf 1000000009#define mod 2010516623LLusing namespace std;inline int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();    return x*f;}int n,k;ll m,sum;bool vis[50005];struct node{int p,c;}a[50005];priority_queue<int,vector<int>,greater<int> >q1;priority_queue<pa,vector<pa>,greater<pa> >q2,q3;bool cmp(node a,node b){return a.c<b.c;}int main(){    n=read();k=read();scanf("%lld",&m);    for (int i=1;i<=n;i++){a[i].p=read();a[i].c=read();}    sort(a+1,a+n+1,cmp);    for (int i=1;i<=k;i++)    {        sum+=a[i].c;if (sum>m) return printf("%d",i-1),0;        q1.push(a[i].p-a[i].c);    }    if (k==n) return printf("%d",k),0;    for (int i=k+1;i<=n;i++)    {        q2.push(make_pair(a[i].c,i));        q3.push(make_pair(a[i].p,i));    }    for (int i=k+1;i<=n;i++)    {        while (vis[q2.top().second]) q2.pop();        while (vis[q3.top().second]) q3.pop();        int x=q2.top().second;        int y=q3.top().second;        int A=q1.top()+q2.top().first;        int B=q3.top().first;        if (A<B){sum+=A;q1.pop();q1.push(a[x].p-a[x].c);q2.pop();vis[x]=1;}        else{sum+=B;q3.pop();vis[y]=1;}        if (sum>m) return printf("%d",i-1),0;    }    printf("%d",n);    return 0;}
原创粉丝点击