• 中大周赛15年第6场


    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
  • 相关阅读:
    VisionPro CogDistanceSegmentSegmentTool工具的功能原理
    VisionPro CogDistanceSegmentLineTool工具
    VisionPro CogDistanceSegmentEllipseTool工具 几何测量工具
    VisionPro CogDistanceSegmentCircleTool工具 几何测量工具
    VisionPro CogDistancePointPointTool 几何测量工具
    VisionPro CogDistancePointSegmentTool工具 几何测量工具
    VisionPro CogDistancePointLineTool工具
    VisionPro CogDistancePointCircleTool工具
    VisionPro CogDistancePointEllipseTool工具
    VisionPro CogDistanceLineEllipseTool 几何测量工具
  • 原文地址:https://www.cnblogs.com/-maybe/p/4418829.html
Copyright © 2020-2023  润新知