【洛谷 3093】[USACO13DEC]牛奶调度Milk Scheduling

来源:互联网 发布:交通银行软件开发待遇 编辑:程序博客网 时间:2024/06/06 03:50

题目描述

Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes only one unit of time to milk.

Being impatient animals, some cows will refuse to be milked if Farmer John waits too long to milk them. More specifically, cow i produces g_i gallons of milk (1 <= g_i <= 1000), but only if she is milked before a deadline at time d_i (1 <= d_i <= 10,000). Time starts at t=0, so at most x total cows can be milked prior to a deadline at time t=x.

Please help Farmer John determine the maximum amount of milk that he can obtain if he milks the cows optimally.

FJ有N(1 <= N <= 10,000)头牛要挤牛奶,每头牛需要花费1单位时间。

奶牛很厌烦等待,奶牛i在它的截止时间d_i (1 <= d_i <= 10,000)前挤g(1 <= g_i <= 1000)的奶,否则将不能挤奶。时间t开始时为0,即在时间t=x时,最多可以挤x头奶牛。

请计算FJ的最大挤奶量。

输入输出格式

输入格式:
Line 1: The value of N.

Lines 2..1+N: Line i+1 contains the integers g_i and d_i.
输出格式:
Line 1: The maximum number of gallons of milk Farmer John can obtain.
输入输出样例

输入样例#1:
4
10 3
7 5
8 1
2 1
输出样例#1:
25
说明

There are 4 cows. The first produces 10 gallons of milk if milked by time 3, and so on.

Farmer John milks cow 3 first, giving up on cow 4 since she cannot be milked by her deadline due to the conflict with cow 3. Farmer John then milks cows 1 and 2.
奇妙的贪心。跟之前的建筑抢修有区别。
根据产奶量建立一个小根堆。先按照截止时间从小到大排个序,依次放入小根堆中,每次放时间+1.如果发现当前截止时间< =t,说明放不下了,这时候从将堆顶元素的产奶量与当前元素比较。如果大于当前,则替换,保证最优。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn=100000+10;int n,sum;struct node{    int g,d;}a[maxn];bool cmp(node a,node b){    return a.d<b.d;}bool operator < (node a,node b){    return a.g>b.g;//小根堆 }priority_queue<node>q;int main(){    int t=0;     scanf("%d",&n);    for(int i=1;i<=n;i++)    {        scanf("%d%d",&a[i].g,&a[i].d);    }    sort(a+1,a+1+n,cmp);//按截止时间从小到大排序     q.push(a[1]);    sum+=a[1].g;//总产奶量    t++;    for(int i=2;i<=n;i++)    {        node f=q.top();        if(t<a[i].d)        {            q.push(a[i]);            sum+=a[i].g;            t++;        }        else if(a[i].g>f.g&&t>=a[i].d)        {            q.pop();            q.push(a[i]);            sum+=a[i].g;            sum-=f.g;        }    }    printf("%d\n",sum);    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狗狗受凉吐了怎么办 狗狗咳嗽流鼻涕一直不好怎么办 宝宝感冒咳嗽流鼻涕发烧怎么办 狗狗感冒咳嗽流鼻涕怎么办 9岁儿童咳嗽鼻塞怎么办 三个月大的狗狗流鼻涕怎么办 3个月小狗干呕流鼻涕怎么办 狗狗流鼻涕怎么办有浓 小狗狗感冒了怎么办呢 狗狗一直擤鼻涕怎么办 小狗感冒流黄鼻涕怎么办 六个月宝宝鼻塞流鼻涕怎么办 小狗吃太多吐了怎么办 狗狗晕车一直吐怎么办 狗狗已经晕车了怎么办 狗狗得犬瘟怎么办 泰迪坐车吐了怎么办 小孩感冒流鼻涕带血怎么办 孩子鼻子流鼻涕有血丝怎么办 鼻子过敏流鼻涕有血丝怎么办 孕妇感冒头痛鼻涕带血怎么办 孕妇感冒鼻塞鼻涕带血怎么办 孕晚期感冒流鼻涕打喷嚏怎么办 孕晚期感冒鼻塞流鼻涕怎么办 怀孕初期鼻涕一直流怎么办 孩子一直流鼻水怎么办 9个月宝宝流鼻涕怎么办 8个月婴儿流鼻涕怎么办 3岁宝宝鼻塞咳嗽怎么办 又感冒又咳嗽了怎么办 鼻塞有一个月了怎么办 感冒一直流清水鼻涕怎么办 孩子受凉流清水鼻涕怎么办 一遇冷空气就打喷嚏流鼻涕怎么办 打喷嚏鼻塞流清鼻涕怎么办 哺乳期鼻子不通气有鼻涕怎么办 宝宝热伤风流清鼻涕怎么办 哺乳期感冒流鼻涕怎么办最有效 小孩睡觉太晚了怎么办 孩子晚上睡觉不安稳怎么办 月子里宝宝睡觉不安稳怎么办