【BZOJ】3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者(贪心)

http://www.lydsy.com/JudgeOnline/problem.php?id=3410

太神了。。。。

按末端点排序然后贪心取即可。

QAQ

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <iostream>

#include <algorithm>

#include <queue>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)

#define for1(i,a,n) for(int i=(a);i<=(n);++i)

#define for2(i,a,n) for(int i=(a);i<(n);++i)

#define for3(i,a,n) for(int i=(a);i>=(n);--i)

#define for4(i,a,n) for(int i=(a);i>(n);--i)

#define CC(i,a) memset(i,a,sizeof(i))

#define read(a) a=getint()

#define print(a) printf("%d", a)

#define dbg(x) cout << #x << " = " << x << endl

#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }

inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

inline const int max(const int &a, const int &b) { return a>b?a:b; }

inline const int min(const int &a, const int &b) { return a<b?a:b; }

struct dat { int x, y; }a[50005];

int n;

bool cmp(const dat &a, const dat &b) { return a.y<b.y; }

int main() {

    read(n);

    for1(i, 1, n) read(a[i].x), read(a[i].y);

    sort(a+1, a+1+n, cmp);

    int ans=0, ed=0;

    for1(i, 1, n) {

        if(a[i].x>=ed) {

            ++ans;

            ed=a[i].y;

        }

    }

    print(ans);

    return 0;

}

 

 


 

 

Description

    约翰有N(1≤N≤50000)头牛,约翰的草地可以认为是一条直线.每只牛只喜欢在某个特定的范围内吃草.第i头牛喜欢在区间(Si,Ei)吃草,1≤Si<Ei≤1,000,000,00.
    奶牛们都很自私,他们不喜欢和其他奶牛共享自己喜欢吃草的领域,因此约翰要保证任意
两头牛都不会共享他们喜欢吃草昀领域.如果奶牛i和奶牛J想要同时吃草,那么要满足:Si>=Ej或者Ei≤Sj.约翰想知道在同一时刻,最多可以有多少头奶牛同时吃草?

Input

    第1行:一个整数N.
    第2到N+1行:第i+l行有两个整数Si,Ei.

Output

 
    一个整数,最多可以有多少头牛同时吃草.

Sample Input

5
2 4
1 12
4 5
7 10
7 8

Sample Output

3

HINT

  第1,3,4共3只奶牛可以同时吃草,第1,3,5也可以.

Source

你可能感兴趣的:(USACO)