Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
赋错一个值,结果一夜没睡好,早上早早起来发现了。O(n)复杂度#include<iostream> using namespace std; struct Cow { int l; int r; }; int cmp(const void * Aa,const void * Bb) { Cow A = *(Cow *)Aa; Cow B = *(Cow *)Bb; if(A.l == B.l) return A.r - B.r; return A.l - B.l; } int main() { int nums,T; scanf("%d%d",&nums,&T); Cow cow[25000]; for(int i=0;i<nums;i++) { scanf("%d%d",&cow[i].l,&cow[i].r); } qsort(cow,nums,sizeof(Cow),cmp); if(cow[0].l != 1) { printf("%d\n",-1); return 1; } int begin=0; while(cow[begin].l==1){begin++;} int right = cow[begin-1].r; int max = right; int count_cows =1; for(int i=begin;i<nums;i++) { if(cow[i].l <= right+1) { if(cow[i].r > max ) { max = cow[i].r; if(max == T) { count_cows++; break; } if(i==nums-1) count_cows++; } } else { if(cow[i].l-max>1) { printf("%d\n",-1); return 1; } count_cows++; right = max; i--; } } if(max == T) printf("%d\n",count_cows); else printf("%d\n",-1); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator