题目描述
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.
倒序按时间循环,把所有合法的牛插入堆中,每个时间取出最大值。
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> a[10010];
priority_queue<int> q;
int main()
{int i,j,k,m,n,p,x,y,z,ans=0;scanf("%d",&n);m=0;for (i=1;i<=n;i++){scanf("%d%d",&x,&y);m=max(m,y);a[y].push_back(x);}for (j=m;j;j--){for (i=0;i<a[j].size();i++)q.push(a[j][i]);if (!q.empty()){ans+=q.top();q.pop();}}printf("%d\n",ans);
}