zoukankan      html  css  js  c++  java
  • 洛谷 P2970 [USACO09DEC]自私的放牧Selfish Grazing

    题目描述

    Each of Farmer John's N (1 <= N <= 50,000) cows likes to graze in a certain part of the pasture, which can be thought of as a large one-dimeensional number line. Cow i's favorite grazing range starts at location S_i and ends at location E_i (1 <= S_i < E_i; S_i < E_i <= 100,000,000).

    Most folks know the cows are quite selfish; no cow wants to share any of its grazing area with another. Thus, two cows i and j can only graze at the same time if either S_i >= E_j or E_i <= S_j. FJ would like to know the maximum number of cows that can graze at the same time for a given set of cows and their preferences.

    Consider a set of 5 cows with ranges shown below:

      ... 1    2    3    4    5    6    7    8    9   10   11   12   13 ...
      ... |----|----|----|----|----|----|----|----|----|----|----|----|----
    Cow 1:      <===:===>          :              :              :
    Cow 2: <========:==============:==============:=============>:
    Cow 3:          :     <====>   :              :              :
    Cow 4:          :              :     <========:===>          :
    Cow 5:          :              :     <==>     :              :

    These ranges represent (2, 4), (1, 12), (4, 5), (7, 10), and (7, 8), respectively.

    For a solution, the first, third, and fourth (or fifth) cows can all graze at the same time. If the second cow grazed, no other cows could graze. Also, the fourth and fifth cows cannot graze together, so it is impossible for four or more cows to graze.

    约翰有N(1≤N≤50000)头牛,约翰的草地可以认为是一条直线.每只牛只喜欢在某个特定的范围内吃草.第i头牛喜欢在区间(Si,Ei)吃草,1≤Si<Ei≤1,000,000,00.

    奶牛们都很自私,他们不喜欢和其他奶牛共享自己喜欢吃草的领域,因此约翰要保证任意

    两头牛都不会共享他们喜欢吃草昀领域.如果奶牛i和奶牛J想要同时吃草,那么要满足:Si>=Ej或者Ei≤Sj.约翰想知道在同一时刻,最多可以有多少头奶牛同时吃草?

    输入输出格式

    输入格式:

     

    • Line 1: A single integer: N

    • Lines 2..N+1: Line i+1 contains the two space-separated integers: S_i and E_i

     

    输出格式:

     

    • Line 1: A single integer representing the maximum number of cows that can graze at once.

     

    输入输出样例

    输入样例#1: 复制
    5 
    2 4 
    1 12 
    4 5 
    7 10 
    7 8 
    
    输出样例#1: 复制
    3 
    思路:贪心
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n,v,ans;
    struct nond{
        int a,b;
    }f[1000005];
    bool cmp(nond x,nond y){
        return x.b<y.b;
    }
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&f[i].a,&f[i].b);
        sort(f,f+n,cmp);
        for(int i=1;i<=n;i++)
            if(i==1||f[i].a>=f[v].b){ ans++;v=i; }
        printf("%d
    ",ans);
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    asp.net中3个常用的功能,直接上代码
    看WEB 2.0实战书中的部分技术资料 引用 很随笔,很杂乱
    Building a Web 2.0 Portal with ASP.NET 3.5(DropThings实例教学)
    如何防治猪流感!猪流感!?会像SARS那样爆发吗?我们能作的就是预防。
    招聘!北京 DNN程序员 5K7K
    国外的机器人爱好者使用C#等研发的一种家庭灭火机器人
    OLAP与OLTP的区别 及 什么是数据仓库
    动态绑定数据源的ReportViewer终于搞定了
    牛奶再次出事?!算了,我还是自己榨豆浆吧。中国人喝豆浆!
    c#遍历HashTable
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8149756.html
Copyright © 2011-2022 走看看