AtCoder Beginner Contest 103 D - Islands War

该问题描述了一个由N个岛屿和N-1座桥梁组成的网络,其中发生了M次岛屿之间的纠纷。每起纠纷请求断开两个特定岛屿之间的连接。目标是找出满足所有纠纷请求所需的最少桥梁拆除数量。给出N、M和各纠纷的岛屿编号,求解最小拆除桥梁数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time limit : 2sec / Memory limit : 1024MB

Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N−1 bridges.

The i-th bridge connects the i-th island from the west and the (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: A dispute took place between the ai-th island from the west and the 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 requests.

Find the minimum number of bridges that must be removed.

Constraints

  • All values in input are integers.
  • 2≤N≤105
  • 1≤M≤105
  • 1≤ai<biN
  • All pairs (ai,bi) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a1 b1
a2 b2
:
aM bM

Output

Print the minimum number of bridges that must be removed.


Sample Input 1

Copy

5 2
1 4
2 5

Sample Output 1

Copy

1

The requests can be met by removing the bridge connecting the second and third islands from the west.


Sample Input 2

Copy

9 5
1 8
2 7
3 5
4 6
7 9

Sample Output 2

Copy

2

Sample Input 3

Copy

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

Sample Output 3

Copy

4

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
struct node{
	int l,r;
}a[100000+20];
bool cmp(node a,node b)
{
	if(a.r==b.r)
		return a.l<b.l;//右边相同比较左边
	return a.r<b.r;//对数据右排序
}
int main ()
{
	int n,m,i;
	scanf("%d%d",&n,&m);
	for(i=0;i<m;i++)
		scanf("%d %d",&a[i].l,&a[i].r);
	int mn=a[0].r;
	for(i=0;i<m;i++)
		mn=min(a[i].r,mn);
		
	sort(a,a+m,cmp);//排序
int now=mn,ans=1;//找到最小的
	for(i=1;i<m;i++)
	{
		if(a[i].l>=now)
		{
			now=a[i].r;//如果下一个数据的左边大于切断桥的右边则仍然需要切桥
			ans++;
		}

	}
	printf("%d\n",ans);
	return 0;
}

切的桥始终在右区间的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值