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

    思路:

    首先,将所有的区间按照右端点排序;然后依次每局每个区间,如果当前区间不与下一个区间冲突,则使答案+1;

    代码:

    // luogu-judger-enable-o2
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    
    long long read()
    {
        long long x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    struct GRASS{
        int l,r;
    }grass[50008];
    
    int cmp(GRASS x,GRASS y)
    {
        if(x.r^y.r) return x.r<y.r; 
        else return x.l>y.l;
    } 
    
    int n,max_left,now,ans=1; 
    
    int main()
    {
        n=read();
        for(int i=1;i<=n;++i)
        {
            grass[i].l=read();
            grass[i].r=read();
            max_left=max(max_left,grass[i].r);
        }
        sort(grass+1,grass+n+1,cmp);
        now=grass[1].r;
        for(int i=2;i<=n;++i)
        {
            if(grass[i].l>=now)
            {
                ans++;
                now=grass[i].r;
            }
        }
        printf("%d",ans);
        return 0;
    } 
  • 相关阅读:
    oracle 静默安装
    浅析hybrid模式下地支付宝钱包和微信
    LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解
    hdu 5411 CRB and Puzzle 矩阵高速幂
    Hadoop作业性能指标及參数调优实例 (三)Hadoop作业性能參数调优方法
    实现Android4.4系统设置分页滑动浏览功能
    oracle 数据库中数据导出到excel
    Nginx配置upstream实现负载均衡
    公司须要内部的地图服务,准备自己去开发可是成本太高,如今有没有专门为企业提供GIS地图开发的产品呀?大概价格多少?
    图片在内存中的占用的空间大小
  • 原文地址:https://www.cnblogs.com/-hhs/p/11196073.html
Copyright © 2011-2022 走看看