牛券Cow Coupons

USACO12FEB

久违的奶牛题。

题意:

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

解法:

在ZR时摸鱼王讲的一道贪心题。
但这道题并不是一道裸贪心,直接对 $ C $ 排序,取前 $ k $ 个数并不完全对,具体为什么自己想想。
正确的做法依旧是贪心,不过是可以反悔的贪心。
我们优先处理使用优惠券之后最便宜的几头牛,然后选择剩下的牛中不用券最便宜的,之后判断要不要将用过的一张券转用给一头新的牛。
具体做法就是开一个大根堆,每次维护 $ price_i - cost_i $ 就可以了。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

#define LL long long
#define N 50010

LL n,k,m;

struct cow {
    LL price,cost;
} node[N];
inline bool cmp1(cow a,cow b) {
    return a.cost < b.cost;
}
inline bool cmp2(cow a,cow b) {
    return a.price < b.price;
}
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].price,&node[i].cost);
    sort(node + 1,node + n + 1,cmp1);
    LL sum = 0;
    for(int i = 1 ; i <= k ; i++) {
        sum += node[i].cost;
        if(sum > m) {
            printf("%d \n",i - 1);
            //system("pause");
            return 0;
        }
        q.push(node[i].price - node[i].cost);
    }
    sort(node + k + 1,node + n + 1,cmp2);
    for(int i = k + 1 ; i <= n ; i++) {
        int u = node[i].price - node[i].cost;
        if(u > q.top()) {
            sum += q.top();
            q.pop();
            q.push(u);
            sum += node[i].cost;
        }
        else sum += node[i].price;
        if(sum > m) {
            printf("%d \n",i - 1);
            //system("pause");
            return 0;
        }
    }
    printf("%lld \n",n);
    //system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/Repulser/p/11402679.html

时间: 2024-08-29 18:52:55

牛券Cow Coupons的相关文章

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 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

[USACO12FEB]牛券Cow Coupons(堆,贪心)

[USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 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), b

[USACO12FEB]牛券Cow Coupons

嘟嘟嘟 这其实是一道贪心题,而不是dp. 首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的物品更划算,是的话就更新. 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include&

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far

洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at

P2419 [USACO08JAN]牛大赛Cow Contest

P2419 [USACO08JAN]牛大赛Cow Contest 海星 这题代码比较短 (哪题Floyd代码长的) 太真实了 直接上代码吧 这题就是一个经典的传递闭包问题 可以用拓扑排序啥的 不过还是Floyd简便一下 原文地址:https://www.cnblogs.com/qf-breeze/p/10473684.html

牛大赛Cow Contest

题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competito

P2952 [USACO09OPEN]牛线Cow Line

题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a line. The line begins with no cows and then, as time progresses, one by one, the cows join the line on the left or right side. Every once in a while, some number of cows on the left

牛线Cow Line

题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line numb