群岛大战
英文题目:
Problem Statement
There are N N N islands lining up from west to east, connected by N − 1 N−1 N−1 bridges.
The i − t h i-th i−th bridge connects the i − t h i-th i−th island from the west and the ( i + 1 ) − t h (i+1)-th (i+1)−th island from the west.
One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:
Request i i i: A dispute took place between the a i − t h a_i-th ai−th island from the west and the b i − t h b_i-th bi−th island from the west. Please make traveling between these islands with bridges impossible.
You decided to remove some bridges to meet all these M M M requests.
Find the minimum number of bridges that must be removed.
Constraints
All values in input are integers.
2
≤
N
≤
1
0
5
2 \leq N \leq 10^5
2≤N≤105
1
≤
M
≤
1
0
5
1 \leq M \leq 10^5
1≤M≤105
1
≤
a
i
<
b
i
≤
N
1 \leq a_i < b_i \leq N
1≤ai<bi≤N
All pairs
(
a
i
,
b
i
)
(a_i, b_i)
(ai,bi) are distinct.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of bridges that must be removed.
Sample 1
Input
Output
The requests can be met by removing the bridge connecting the second and third islands from the west.
Sample 2
Input
Output
Sample 3
Input
Output
中文题目:
问题陈述
这里有 N N N岛屿,从西向东排列,由 N − 1 N−1 N−1桥梁连接。
i − t h i-th i−th桥连接了西面的 i − t h i-th i−th岛和西面的 ( i + 1 ) − t h (i+1)-th (i+1)−th岛。
一天,一些岛屿之间发生了争端,岛上的居民提出了M个请求:
请求 i i i:在西面的 a i − t h a_i-th ai−th岛和西面的 b i − t h b_i-th bi−th岛之间发生了争端。请不要在这些岛屿之间搭桥。
您决定删除一些桥以满足所有这些 M M M请求。
找出必须拆除的桥的最小数量。
约束
输入中的所有值都是整数。
2
≤
N
≤
1
0
5
2 \leq N \leq 10^5
2≤N≤105
1
≤
M
≤
1
0
5
1 \leq M \leq 10^5
1≤M≤105
1
≤
a
i
<
b
i
≤
N
1 \leq a_i < b_i \leq N
1≤ai<bi≤N
所有对
(
a
i
,
b
i
)
(a_i, b_i)
(ai,bi)都是不同的。
##输入
标准输入的输入格式如下:
输出
打印必须移除的桥的最小数量。
样本1
输入
输出
通过拆除连接西部第二和第三岛屿的桥梁,可以满足这些要求。
样本2
输入
输出
示例3
输入
输出
代码
#include<bits/stdc++.h>
using namespace std;
struct node{int x,y; } a[1200000];
int n,m,s,mi;
bool cmp(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int main()
{
cin >>n >>m;
for (int i=1;i<=m;i++)
{
cin >>a[i].x >>a[i].y;
mi=min(mi,a[i].y);
}
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++)
{
if(a[i].x>=mi)
{
s++;
mi=a[i].y;
}
}
cout <<s;
return 0;
}