| ||||||||||
| 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 | |||||||||
有点陷阱啊,其实就是自己不小心了。带数据(后边大牛的)input:
10 10
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 10
10 10
output: 4
拿那位大牛的话说,前边那头傻牛是1--3;
后边那头蛮牛就可以是4----6,不必是由3开始。
code;
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int start;
int end;
bool operator<(const node &a) const
{
if(start<a.start)
return true;
else
{
if(start==a.start)
{
if(end>a.end)
return true;
}
}
return false;
}
}a[25001];
int main()
{
int n,shift,i,MAX,MIN,loop,s,e,next,ans,flag;
while(cin>>n>>shift)
{
MIN=0x7fffffff;
MAX=0;
for(i=0;i<n;i++)
{
cin>>a[i].start>>a[i].end;
if(a[i].start<MIN)
MIN=a[i].start;
if(a[i].end>MAX)
MAX=a[i].end;
}
if(MAX<shift||MIN>1)
{
cout<<"-1"<<endl;
continue;
}
sort(a,a+n);
s=a[0].end;
e=a[0].end;
loop=0; ans=1;next=0;
while(e<shift)
{
flag=0;
for(i=loop;i<n;i++)
{
if(a[i].start<=s+1&&a[i].end>e)//加上一个1就OK了
{
flag=1;
loop=i;
e=a[i].end;
}
if(a[i].start>s)
{
next=i;
break;
}
}
if(flag==0)
break;
else
ans++;
//cout<<loop<<" "<<next<<endl;
s=a[loop].end;
e=a[loop].end;
loop=next;
}
if(e>=shift)
cout<<ans<<endl;
else
cout<<-1<<endl;
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator