P2970 [USACO09DEC] selfish grazing Selfish Grazing

 If garbled, please click .

 

Title Description

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.

John has N (1≤N≤50000) cattle, grass John can be considered a straight line. Each graze like cattle within a specific range. Grazing cattle like the i-th segment (Si, Ei), 1≤Si <Ei≤1,000,000,00.

Cows are very selfish, they do not like to share their favorite cows and other grazing areas, and therefore to ensure that any John

Two cows will not graze Yun shared their favorite field. Cows and Cows J i If desired while graze, it should satisfy: Si> = Ej or Ei≤Sj. John wanted to know at the same time, maximum number of cows grazing at the same time?

Input Format

* Line 1: A single integer: N

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

Output Format

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

Sample input and output

Input # 1
5 
2 4 
1 12 
4 5 
7 10 
7 8 
Output # 1
3 

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int n,t,ans=1;
struct node{
	int x,y;
}a[50005];
int read(){
	int a=0,b=1;
	char ch=getchar();
	while((ch<48||ch>57)&&ch!='-'){
		ch=getchar();
	}
	if(ch=='-'){
		b=-1;
		ch=getchar();
	}
	while(ch<48||ch>57){
		ch=getchar();
	}
	while(ch>47&&ch<58){
		a=a*10+ch-48;
		ch=getchar();
	}
	return a*b;
	Return x <y;
}

bool cmp(node x,node y){
}
int main(){
    n=read();
    for(int i=1;i<=n;i++){
    	a[i].x=read(),a[i].y=read();
	}
    sort(a+1,a+n+1,cmp);
    t=a[1].y;
    for(int i=2;i<=n;i++){
		if(a[i].x>=t){
			t=a[i].y;
			ans++;
		}
	}
    printf("%d",ans);
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/xiongchongwen/p/11567003.html