AtCoder Beginner Contest 103 —D Islands War

本文介绍了一个算法问题“D-岛屿战争”,该问题要求通过拆掉最少数量的桥来解决岛屿间的纠纷请求。文章提供了一种有效的解决方案,并附带了完整的C++代码实现。

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

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值