码迷,mamicode.com
首页 > 其他好文 > 详细

中大周赛15年第6场

时间:2015-04-12 01:17:50      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

13983. Milk Scheduling

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

 

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.

 

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

4
10 3
7 5
8 1
2 1

Sample Output

25

Hint

 

In the sample, 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.

 

首先,对g排序(大到小),g相同对d排序(小到大)

vis[i] 表示在i时刻有没有喂牛。

遍历一遍,对于每一个cow[i].d,由于cow[i].g 是大到小的,我们尽可能喂她,

什么情况下可以喂呢?

若vis[ 1到cow[i].d ] 之间还有时刻没有喂牛,则可以喂她咯。

有多个时刻可以呢?我们当然希望留出更多的空间给cow[j].d小的,所以尽可能放在后面喂

所以:

 

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 namespace std;
 5 const int maxn=10010;
 6 struct Cow
 7 {
 8     int g,d;
 9 }cow[maxn];
10 bool vis[maxn];
11 bool cmp(Cow a,Cow b)
12 {
13     if(a.g==b.g)
14         return a.d<b.d;
15     return a.g>b.g;
16 }
17 int main()
18 {
19     int n;
20     while(cin>>n)
21     {
22         for(int i=1;i<=n;i++)
23         {
24             cin>>cow[i].g>>cow[i].d;
25         }
26         sort(cow+1,cow+n+1,cmp);
27         memset(vis,false,sizeof(vis));
28         int sum=0;
29         for(int i=1;i<=n;i++)
30         {
31             int j=cow[i].d;
32             while(vis[j]&&j>0)
33                 j--;
34             if(j>0)
35             {
36                 vis[j]=true;
37                 sum+=cow[i].g;
38             }
39         }
40         cout<<sum<<endl;
41     }
42     return 0;
43 }
View Code

 

中大周赛15年第6场

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4418829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!