Description
由于912的大牛们太专注于编程,以至于经常忘记打扫卫生,为此xiuge专门任命了一位卫生部长——cloudiris,她准备安排大牛们打扫卫生,而大牛们由于太忙了,每个人都只有一段时间可以打扫,而xiuge规定必须要在1到T时间内都保证有人打扫,所以请你帮助卫生部长,来计算最少需要的人数。
Input
输入 1<=N<=25000 和 1<= T<=1,000,000 。
接下来有N行数 ,每行有两个正整数 0< a < = b ,表示第i-th人在时间a到b之间有 时 间 打 扫 卫 生。(注意 该人在 a 时间开始打扫,到b时间结束)
Output
输出保证在T时间内都安排有人打扫的情况下的最少人数。不存在此方案时,输出 -1 。
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Hint
如果选择 1到2 和 3 到4 两段时间,表示在1到4的时间内是连续有人打扫的。
Source
#include<iostream>
#include<stdio.h>
using namespace std;
int quicksort(int *a,int *b,int low,int high)
{
int temp,tempb,mid;
mid=(low+high)/2;
temp=a[mid];
tempb=b[mid];
a[mid]=a[low];
b[mid]=b[low];
a[low]=temp;
b[low]=tempb;
while(low<high)
{
while(low<high && a[high]>temp)
high--;
if(low<high)
{
a[low]=a[high];
b[low]=b[high];
low++;
}
while(low<high && a[low]<temp)
low++;
if(low<high)
{
a[high]=a[low];
b[high]=b[low];
high--;
}
}
b[low]=tempb;
a[low]=temp;
return low;
}
void qsort(int *a,int *b,int low,int high)
{
if(low<high)
{
int temp=quicksort(a,b,low,high);
qsort(a,b,low,temp-1);
qsort(a,b,temp+1,high);
}
}
int main()
{
int i,j,n,t,begin,end,sign,judge,bejudge,count,max,temp,endt,begint;
int a[25001],b[25001];
while(scanf("%d%d",&n,&t)!=EOF)
{
max=0;j=0,temp=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
}
qsort(a,b,0,n-1); //按开始时间快速排序
if(a[0]==1)
{
for(i=0;i<n;i++) //开始时间相同的只保留最晚结束的
{
if(a[i]==a[i+1])
{
if(b[i]>max)
{
max=b[i];
sign=i;
}
continue;
}
if(b[i]>max)
{
max=b[i];
sign=i;
}
max=0;
a[j]=a[sign];
b[j]=b[sign];
j++;
}
for(i=j;i<n;i++)
a[i]=b[i]=0;
begin=a[0];
endt=end=b[0];
count=1;
i=1;
while(i<j+1) //开始判断
{
if(end==t) //如果结束时间为t 跳出
break;
else
{
if((a[i]<=end+1)&&(a[i]>=begin)) //开始时间在本次开始时间与结束时间+1的范围内的,则选取结束时间最晚的
{
if(b[i]>endt)
{
endt=b[i];
begint=a[i];
}
i++;
continue;
}
else if(end!=endt) //如果选中了 则endt不会变化,conut++
{
count++;
end=endt;
begin=begint;
if(end==t)
break;
else
continue;
}
else
{
count=-1;
break;
}
}
}//while
//count++;
}//if
else
count=-1;
cout<<count<<endl;
}//while
return 0;
}
学到的: 复习快排,类似题可以先排序,然后按贪心算法计算~思路比较混乱 需要多加联系