https://ac.nowcoder.com/acm/contest/392#question
华华听月月唱歌
贪心
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 100005 9 #define eps 1e-8 10 #define pi acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<char,int> pci; 15 typedef pair<pair<int,string>,pii> ppp; 16 typedef unsigned long long ull; 17 const long long MOD=1e9+9; 18 /*#ifndef ONLINE_JUDGE 19 freopen("1.txt","r",stdin); 20 #endif */ 21 22 int n,m; 23 vector<pii>ve; 24 25 bool cmp(pii a,pii b){ 26 if(a.first==b.first) return a.second>b.second; 27 return a.first<b.first; 28 } 29 30 int main(){ 31 #ifndef ONLINE_JUDGE 32 // freopen("1.txt","r",stdin); 33 #endif 34 std::ios::sync_with_stdio(false); 35 cin>>n>>m; 36 int u,v; 37 for(int i=1;i<=m;i++){ 38 cin>>u>>v; 39 ve.pb(make_pair(u,v)); 40 } 41 sort(ve.begin(),ve.end(),cmp); 42 int ans=1; 43 int last=ve[0].second; 44 int tmp=ve[0].second; 45 for(int i=1;i<ve.size();i++){ 46 if(ve[i].first<=last) { 47 tmp=max(ve[i].second,tmp); 48 } 49 else{ 50 if(ve[i].first-last<=1){ 51 last=ve[i].second; 52 tmp=last; 53 ans++; 54 } 55 else if(ve[i].first-tmp<=1){ 56 ans+=1; 57 last=tmp; 58 i--; 59 } 60 else{ 61 cout<<-1<<endl; 62 return 0; 63 } 64 } 65 } 66 if(last!=n&&tmp==n) ans++; 67 cout<<ans<<endl; 68 }