AtCoder Beginner Contest 103 D
题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连.
按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <queue> 7 #include <map> 8 #include <stack> 9 #define ll long long 10 #define out(a) printf("%d",a) 11 #define writeln printf(" ") 12 using namespace std; 13 const int N=1e5+50; 14 int n,m; 15 int now,ans; 16 struct node 17 { 18 int x,y; 19 }a[N]; 20 int read() 21 { 22 int s=0,t=1; char c; 23 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 24 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 25 return s*t; 26 } 27 ll readl() 28 { 29 ll s=0,t=1; char c; 30 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 31 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 32 return s*t; 33 } 34 bool cmp(node a,node b) 35 { 36 return a.y==b.y?a.x<b.x:a.y<b.y; 37 } 38 int main() 39 { 40 n=read(),m=read(); now=0; 41 for (int i=1;i<=m;i++) 42 a[i].x=read(),a[i].y=read(); 43 sort(a+1,a+m+1,cmp); 44 for (int i=1;i<=m;i++) 45 if (now>a[i].x&&now<=a[i].y) continue; 46 else ans++,now=a[i].y; 47 out(ans); 48 return 0; 49 }