这个问题我用的一个线段区间来表示滴:
像这个图:
我举例的数据 :
2 3 4
4 5 5
1 8 4
5 2 10
这是转化的图:如 2 3 4 (3为头坐标,第几天居住加连住天数为3+4为尾坐标)
想一想有一条竖直的线从头2扫描到尾10,当竖线的坐标为6时,第一条线段、第二条线段、第4条线段与其相交。是不是就表示第一组数据和第二组数据还有第四组数据是并行的,而第三组数据在此时不用。此时所用的房间数SUM=11.令temp=max(sum);那temp是不是最小所需房间数?
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
struct Node{
introom;
intwhen;
intday;
}s[10000];
int com(const void *,const void *);
int main()
{
inti,j;
intT;
intN;
intsum;
intmax,min;
inttemp;
scanf("%d",&T);
while(T--)
{
temp=0;
max=0;
min=10001;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d%d%d",&s[i].room,&s[i].when,&s[i].day);
if(max<s[i].day+s[i].when)
max=s[i].day+s[i].when;
if(min>s[i].when)
min=s[i].when;
}
qsort(s,N,sizeof(structNode),com);
for(i=min;i<=max;i++)
{
sum=0;
for(j=0;j<N;j++)
{
if((i>=s[j].when)&&(i<s[j].when+s[j].day))
sum+=s[j].room;
if(i<s[j].when)
break;
}
if(temp<sum)
temp=sum;
}
printf("%d\n",temp);
}
return0;
}
int com(const void *a,const void *b)
{
structNode *c;
structNode *d;
c=(structNode *)a;
d=(structNode *)b;
if(c->when!=d->when)
returnc->when-d->when;
elsereturn (c->when+c->day)-(d->when+d->day);
}