13983. Milk Scheduling Constraints
Time limit:1 secs, Memory limit:256 MB
Description
Farmer John has N cows this need to being milked (1 <= N <=), each of the which takes only one unit of time to milk.
Being impatient animals, some cows would refuse to being milked if Farmer John waits too long to milk them. More specifically, cow I produces g_i gallons of milk (1 <= g_i <=), but only if she's milked before a Deadlin E at time d_i (1 <= d_i <= 10,000). Time starts at t=0, so at the most x total cows can is milked prior to a deadline at time T=x.
Farmer John determine the maximum amount of milk that he can obtain if he milks the cows optimally.
Input
* Line 1:the value of N.
* Lines 2..1+n:line i+1 contains the integers g_i and d_i.
Output
* Line 1:the Maximum number of gallons of milk Farmer John can obtain.
Sample Input
410 37 58) 12 1
Sample Output
25
Hint
In the sample, there is 4 cows. The first produces 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 being milked by hers deadline due to the conflict with cow 3. Farmer John then milks cows 1 and 2.
First, sort g (large to small), g the same to D sort (small to large)
Vis[i] Indicates that there are no cows to feed at the moment.
Traversing over, for every cow[i].d, as COW[I].G is big to small, we feed her as much as we can,
Under what circumstances can I feed it?
If there is no time to feed the cows between vis[1 and COW[I].D], you can feed her.
How many moments can it be? Of course we want to set aside more space for cow[j].d, so keep it in the back as much as possible.
So:
int j=cow[i].d;
while (vis[j]&&j>0)
j--;
if (j>0)
{
Vis[j]=true;
SUM+=COW[I].G;
}
1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intmaxn=10010;6 structCow7 {8 intg,d;9 }COW[MAXN];Ten BOOLVIS[MAXN]; One BOOLCMP (Cow a,cow B) A { - if(a.g==B.G) - returna.d<B.D; the returnA.g>B.G; - } - intMain () - { + intN; - while(cin>>N) + { A for(intI=1; i<=n;i++) at { -Cin>>cow[i].g>>cow[i].d; - } -Sort (cow+1, cow+n+1, CMP); -memset (Vis,false,sizeof(Vis)); - intsum=0; in for(intI=1; i<=n;i++) - { to intj=cow[i].d; + while(vis[j]&&j>0) -j--; the if(j>0) * { $vis[j]=true;Panax Notoginsengsum+=cow[i].g; - } the } +cout<<sum<<Endl; A } the return 0; +}
View Code
Mid-Big Week, 15, game 6th.