思路:贪心,按左边界递增,然后右边界递增.
不断扩大右边界。
主要细节多:
s可以到-1e6,r尽量开小.
Code:
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<double,double> pii; const int N = 1e5+5; const int M = 1e6+5; const int Mod = 1e9+7; #define pi acos(-1) #define INF 1e8 #define INM INT_MIN #define pb(a) push_back(a) #define mk(a,b) make_pair(a,b) #define dbg(x) cout << "now this num is " << x << endl; #define met0(axx) memset(axx,0,sizeof(axx)); #define metf(axx) memset(axx,-1,sizeof(axx)); #define sd(ax) scanf("%d",&ax) #define sld(ax) scanf("%lld",&ax) #define sldd(ax,bx) scanf("%lld %lld",&ax,&bx) #define sdd(ax,bx) scanf("%d %d",&ax,&bx) #define sddd(ax,bx,cx) scanf("%d %d %d",&ax,&bx,&cx) #define sfd(ax) scanf("%lf",&ax) #define sfdd(ax,bx) scanf("%lf %lf",&ax,&bx) #define pr(a) printf("%d\n",a) #define plr(a) printf("%lld\n",a) struct Node{int L,r;}p[N]; bool cmp(Node a,Node b) { if(a.L == b.L) return a.r < b.r; return a.L < b.L; } int main() { int n,s,t;sddd(n,s,t); for(int i=1;i<=n;++i) sdd(p[i].L,p[i].r); sort(p+1,p+n+1,cmp); int L = s,r = -INF,ans = 0; for(int i=1;i<=n;++i) { if(p[i].r < L) continue; if(p[i].L > L) { ans++; L = r; if(r >= t) break; if(p[i].L <= L) { r = max(r,p[i].r); if(r >= t) { ans++; break; } } else break; } else { r = max(r,p[i].r); if(r >= t) { ans++; break; } } } if(r < t) printf("No Solution\n"); else pr(ans); system("pause"); return 0; }
给下调试数据:
case1:
3 1 10
-1 0
1 9
9 11
case 2:
1 10
1 5
6 9
9 11