2010年7月18日 星期日

1078. Segments

Oh ~ my god!!
Why alway got the WA #2.
Let me think about this question later.

This question describe :
One line have several segment.
Each segment have two point.

Find which segment is big than other.

ie.

------+-----+--------+------+---------------
-3 -2 2 3

So input N = 2 segment and a = -3 3 && b = -2 2.
a include b.
So a this segment is big than b.
so the answer is
2 segment include to each other.
b a ( The order is small segment to big segment.)







Time Limit: 1.0 second
Memory Limit: 16 MB
A number of segments are lying on a line. Every segment is given with the coordinates of its endpoints. Segments are numbered from 1 to N (0 < N < 500). We assume, that one segment is inside another, if the two segments are different, the first one is fully contained in the second one, and their endpoints do not coincide. Write a program, which finds the numbers of the segments in the longest sequence of segments which are contained in. In the sequence, every segment except the last is inside the next segment in the sequence.
Input
The first line contains one integer N. Next, there are N lines, with two integers on every line, which are the coordinates of the left and the right endpoints of the corresponding segment. These coordinates are integers in the interval [–10000, 10000]. We assume that, the given segments are numbered according to their place in the input.
Output
The first line must contain one integer, equal to the number of segments in the found sequence. The following line must contain the numbers of the segments in this sequence. These numbers must be outputted, in the order in which the segments' lengths increase, starting from the smallest. If there are more than one output sequences, write any of them.
Sample
input

4
-2 2
-1 1
-3 3
4 5

output

3
2 1 3






#include
#include
#include

typedef struct{
int right;
int left;
int index;
int win;
int lose;
}segment;


int contain(segment a,segment b){
if (a.left > b.left && a.right < b.right ) return 1;
else return 0;
}


int main(){

int i,j,m,num;
segment point[500],tmp;
memset(point , '\0' , sizeof(point) );

scanf("%d",&num);
if(num <= 0 || num >= 500) exit(0);

for(i = 0 ; i < num ; i++){
scanf("%d %d",&point[i].left , &point[i].right );
if(point[i].left > 10000 || point[i].left < -10000) exit(0);
else if(point[i].right > 10000 || point[i].right < -10000) exit(0);
point[i].index = i;
}

for(i = 0 ; i < num ; i++){
if(point[i].left > point[i].right){
m = point[i].left;
point[i].left = point[i].right;
point[i].right = m ;
}
}
/*
for(i = 0 ; i < num ; i ++){
printf("%2d %2d\n",point[i].left , point[i].right);
}
*/
for (i = 0 ; i < num ; i++){
for(j = 0; j < num; j++){
if( ( i != j ) && ( contain( point[i] , point[j] ) ) ){
/* printf("i = %d j = %d \n" , i, j); */
point[i].lose ++;
point[j].win ++;
}
}
}
/*
for(i = 0 ; i < num ; i ++){
printf("%2d %2d\n",point[i].win , point[i].lose);
}
*/
for(i = 0 ; i < num ; i ++){
for(j = i ; j < num ; j++ ){
if ( point[i].lose < point[j].lose ){

tmp = point[i];
point[i] = point[j];
point[j] = tmp ;
}
}
}

for(i = point[0].lose ; i < num ; i ++){
for(j = i ; j < num ; j++ ){
if ( point[i].win < point[j].win ){
tmp = point[i];
point[i] = point[j];
point[j] = tmp ;
}
}
}

printf("%d\n",point[0].lose + 1);

for(i = 0 ; i <= point[0].lose ; i ++){
printf("%d ",point[i].index+1);
}

return 0;
}

0 意見:

張貼留言