链接:https://www.nowcoder.com/acm/contest/180/C
来源:牛客网
题目描述
小a的国家里有n个城市,其中第i和第i - 1个城市之间有无向道路连接,特殊的,第1个城市仅与第2个城市相连
为了减轻道路维护负担,城市规划局局长MXT给出了m个要求,他想让小a断开一些道路,使得任意1 ≤ i ≤ m ,城市xi不能到达城市yi
同时最小化断开道路的数量。
输入描述:
第一行两个整数n, m,分别表示城市的数量和请求的数量 接下来m行,每行两个整数x,y,表示需要使得x不能到达y
输出描述:
输出一个整数,表示最小断开桥的数量
示例1
输入
复制
4 2 1 3 2 4
输出
复制
1
说明
可以断开(2, 3)城市之间的道路
示例2
输入
复制
4 3 1 3 2 4 1 2
输出
复制
2
说明
可以断开(1, 2) (2, 3)之间的道路
这题真好,....
印象中,进实验室的时候就是考的区间相交,贪心,最少多少个点,能锁定所有的边。
这道题,只需要 左闭右开,就是原题了。
不过这个优化,当时直接对边排序,超时,好,我们忽略左边的点,只管右边,压进vector,还是超时
后来发现,原来只需要保留每个点,最近能到哪里就好了。 优化真的太强了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
//直接读取文件,高级版,但是不能直接输入,只能文件来搞
namespace fastIO {
#define BUF_SIZE 100000
//fread -> read
bool IOerror = 0;
inline char nc() {
//FILE* fp=fopen("123.txt","r");
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if(p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);//从显示器读入的话,改为stdin,文件的话,改为相应的文件指针fp
if(pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int& x) {
char ch;
while(blank(ch = nc()));
if(IOerror) return;
for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
}
inline void print(int x){//long long 类型的输出
static int cnt;
static int a[20];
cnt=0;
do{
a[++cnt]=x%10;
x/=10;
}while(x);
for(int i=cnt;i>=1;i--)putchar(a[i]+'0');
puts("");
}
#undef BUF_SIZE
};
using namespace fastIO;
const int N=1e6+10,M=1e7+10;
int R[N];
int main(){
int n,m;
read(n);read(m);
rep(i,0,n+1)R[i]=n;
rep(i,1,m+1){
int l,r;
read(l);read(r);
R[l]=min(R[l],r-1);
}
int up=0,ans=0;
for(int i=1;i<n;i++){
if(R[i]==n)continue;
if(i>up){
ans++;
up=R[i];
}else{
up=min(up,R[i]);
}
}
print(ans);
return 0;
}