标签:first 输出 namespace cow name break 中间 覆盖 second
贪心,覆盖点问题
思路:
1、排序,按first从小到大排,如果first一样按照second从小到大排
2、first没有1以及second没有T的直接输出-1,结束
3、从头开始找能首尾连上并且每一片尽可能覆盖大的cow,计数。如果中间出现首尾不能连输出-1结束。
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 struct P 5 { 6 long int first; 7 long int second; 8 }; 9 int cmp(P x,P y) 10 { 11 if(x.first==y.first) 12 return x.second<y.second; 13 return x.first<y.first; 14 } 15 int main() 16 { 17 long int n,m; 18 cin>>n>>m; 19 P p[25005]; 20 long int i,j; 21 for(i=0;i<n;i++) 22 cin>>p[i].first>>p[i].second; 23 p[n].first=99999; 24 sort(p,p+n,cmp); 25 for(i=0;i<n;i++) 26 if(p[i].second==m)break; 27 if(p[0].first!=1)cout<<-1<<endl; 28 else if(i==n)cout<<-1<<endl; 29 else 30 { 31 long int count=1; 32 long int pos; 33 P *q=p; 34 while(q->first==1)q++; 35 q--; 36 pos=q->second; 37 long int posi; 38 bool f=0; 39 while(pos!=m) 40 { 41 q++; 42 posi=pos; 43 while(q->first<=pos+1&&posi<m) 44 { 45 if(q->second>posi)posi=q->second; 46 q++; 47 } 48 if(q->first>posi+1&&posi!=m){f=1;break;} 49 q--; 50 pos=posi; 51 count++; 52 } 53 if(f==1)cout<<-1<<endl; 54 else 55 cout<<count<<endl; 56 } 57 return 0; 58 }
标签:first 输出 namespace cow name break 中间 覆盖 second
原文地址:http://www.cnblogs.com/index556/p/6842906.html