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?
FJ准备买一些新奶牛,市场上有N头奶牛(1<=N<=50000),第i头奶牛价格为Pi(1<=Pi<=10^9)。FJ有K张优惠券,使用优惠券购买第i头奶牛时价格会降为Ci(1<=Ci<=Pi),每头奶牛只能使用一次优惠券。FJ想知道花不超过M(1<=M<=10^14)的钱最多可以买多少奶牛?
Line 1: 三个空格隔开的整数N, K, and M.
Lines 2..N+1: 每行2个整数 第 i+1 行 : P_i and C_i.
输出格式:
Line 1: 一个整数, Fj最多可以买奶牛的数目.
4 1 7
3 2
2 2
8 1
4 3
3
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.
这事实上是一个权限题(别问我从哪里得来的题号)
事实上这道题就是一个水
具体思路是把每头牛拆成两头牛,一头需要用牛券,另一头不需要,买了其中一头就不能买另一头(可用vis数组判断)
之后按照价格大小从小到大排序
然后贪心的从小到大选就可以辣!!(牛劵能用则用,不能用就跳过,具体见代码)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
struct w{
ll a,b;
int cc;
}n[2001412];
int m,c,vis [2001412];
ll k;
int cmp(w x,w y){
return x.aint main(){
scanf("%d%d%lld",&m,&c,&k);
for(int i=1;i<=m;i++){
ll aa,bb;
scanf("%lld%lld",&aa,&bb);
n[i*2-1].a=aa;
n[i*2-1].b=i;
n[i*2-1].cc=0;
n[i*2].a=bb;
n[i*2].b=i;
n[i*2].cc=1;
}
int p=1,ans=0;
sort(n+1,n+2*m+1,cmp);
while(k>=n[p].a){
if(n[p].cc==1){
if(c>=1&&!vis[n[p].b]){
c--;
ans++;
vis[n[p].b]=1;
k-=n[p].a;
p++;
continue;
}
p++;
continue;
}else{
if(!vis[n[p].b]){
ans++;
vis[n[p].b]=1;
k-=n[p].a;
p++;
continue;
}
p++;
}
}
if(ans>=m)ans=m;//防越界
printf("%d",ans);
return 0;
}