zoukankan      html  css  js  c++  java
  • Luogu P2970 [USACO09DEC]自私的放牧

     https://www.luogu.org/problemnew/show/P2970

    P2970 [USACO09DEC]自私的放牧

    题目描述

    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 <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    struct haha{
        int l, r;
    }s[50008];
    int n, now_r, tot;
    
    bool cmp(haha a, haha b) {
        if(a.r != b.r) return a.r < b.r;
        return a.l < b.l;
    }
    
    int main() {
        scanf("%d", &n);
        for(int i=1; i<=n; i++) {
            scanf("%d%d", &s[i].l, &s[i].r);
        }
        sort(s+1, s+1+n, cmp);
        now_r = 0;
        for(int i=1; i<=n; i++) {
            if(s[i].l >= now_r) {
                now_r = s[i].r;
                tot++;
            }
        }
        printf("%d
    ", tot);
    }
    作者:wlz
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Java学习---Java代码编写规范
    移动端与Web端疫情数据展示
    Java实现邮箱验证码
    Java实现短信验证码
    利用Jsoup爬取新冠疫情数据并存至数据库
    echarts全国疫情统计可视化地图(第一阶段)
    构建之法阅读笔记04
    HDU 4628 Pieces(状压DP)题解
    ZOJ 2563 Long Dominoes(状压DP)题解
    POJ 2288 Islands and Bridges(状压DP)题解
  • 原文地址:https://www.cnblogs.com/bljfy/p/8717931.html
Copyright © 2011-2022 走看看