D - Islands War
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 400400 points
Problem Statement
There are NN islands lining up from west to east, connected by N−1N−1 bridges.
The ii-th bridge connects the ii-th island from the west and the (i+1)(i+1)-th island from the west.
One day, disputes took place between some islands, and there were MM requests from the inhabitants of the islands:
Request ii: A dispute took place between the aiai-th island from the west and the bibi-th island from the west. Please make traveling between these islands with bridges impossible.
You decided to remove some bridges to meet all these MM requests.
Find the minimum number of bridges that must be removed.
Constraints
- All values in input are integers.
- 2≤N≤1052≤N≤105
- 1≤M≤1051≤M≤105
- 1≤ai<bi≤N1≤ai<bi≤N
- All pairs (ai,bi)(ai,bi) are distinct.
Input
Input is given from Standard Input in the following format:
NN MM a1a1 b1b1 a2a2 b2b2 :: aMaM bMbM
Output
Print the minimum number of bridges that must be removed.
Sample Input 1 Copy
Copy
5 2 1 4 2 5
Sample Output 1 Copy
Copy
1
The requests can be met by removing the bridge connecting the second and third islands from the west.
思路:如出现 3-5,1-7,6-7,的情况,我们只需要处理 3-5,6-7 即可,按照这个想法,首先找到第一个最小的右边 ,这里也就是 5 ,在遍历从小到大判断右边是否比 5 大,即可得到答案。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
struct node{
int x,y;
};
struct node ai[1000000];
bool cmp(node a,node b)
{
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
}
int main()
{
int a,b;
int min1 = 10000000;
cin>>a>>b;
for(int i=1; i<=b; i++)
{
cin>>ai[i].x>>ai[i].y;
min1 = min(min1,ai[i].y);
}
sort(ai+1,ai+b+1,cmp);
int ant = 1;
for(int i=1;i<=b;i++)
{
if(ai[i].x>=min1)
{
ant++;
min1 = ai[i].y;
}
}
cout<<ant<<endl;
return 0;
}