Passing the Message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 116 Accepted Submission(s): 74
Because all kids have different height, Teacher Liu set some message passing rules as below:
1.She tells the message to the tallest kid.
2.Every kid who gets the message must retell the message to his “left messenger” and “right messenger”.
3.A kid’s “left messenger” is the kid’s tallest “left follower”.
4.A kid’s “left follower” is another kid who is on his left, shorter than him, and can be seen by him. Of course, a kid may have more than one “left follower”.
5.When a kid looks left, he can only see as far as the nearest kid who is taller than him.
The definition of “right messenger” is similar to the definition of “left messenger” except all words “left” should be replaced by words “right”.
For example, suppose the height of all kids in the row is 4, 1, 6, 3, 5, 2 (in left to right order). In this situation , teacher Liu tells the message to the 3rd kid, then the 3rd kid passes the message to the 1st kid who is his “left messenger” and the 5th kid who is his “right messenger”, and then the 1st kid tells the 2nd kid as well as the 5th kid tells the 4th kid and the 6th kid.
Your task is just to figure out the message passing route.
Each test case consists of two lines. The first line is an integer N (0< N <= 50000) which represents the number of kids. The second line lists the height of all kids, in left to right order. It is guaranteed that every kid’s height is unique and less than 2^31 – 1 .
2 5 5 2 4 3 1 5 2 1 4 3 5
Case 1: 0 3 0 0 2 4 0 5 0 0 Case 2: 0 2 0 0 1 4 0 0 3 0
#include<cstdio>
#define MAXN 50010
int h[MAXN];
int b[MAXN][2];
void search(int left,int right,int idx,int biao)
{
int max=0,idxl=0;
for(int i=left;i<=right;i++)
if(h[i]<h[idx]&&h[i]>max)
{
max=h[i];
idxl=i;
}
b[idx][biao]=idxl;
if(idxl)
{
search(left,idxl-1,idxl,0);
search(idxl+1,right,idxl,1);
}
/*
if(idxl&&idxl-1>=left) search(left,idxl-1,idxl,0);
if(idxl&&right>=idxl+1) search(idxl+1,right,idxl,1);这是错误的,要递归到每个叶子节点
*/
}
int main()
{
int T,n;
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
printf("Case %d:/n",i);
scanf("%d",&n);
int max=0,idx=0;
for(int j=1;j<=n;j++)
{
scanf("%d",&h[j]);
if(h[j]>max) max=h[j],idx=j;
}
/*if(idx-1>=1) search(1,idx-1,idx,0);
if(idx+1<=n) search(idx+1,n,idx,1);*/
search(1,idx-1,idx,0);
search(idx+1,n,idx,1);
for(int j=1;j<=n;j++)
printf("%d %d/n",b[j][0],b[j][1]);
}
return 0;
}
错误的程序改正后为正确的程序
#include<cstdio>
#define MAXN 50010
int h[MAXN];
int b[MAXN][2];
void search(int left,int right,int idx,int biao)
{
int max=0,idxl=0;
for(int i=left;i<=right;i++)
if(h[i]<h[idx]&&h[i]>max)
{
max=h[i];
idxl=i;
}
b[idx][biao]=idxl;
if(idxl&&idxl-1>=left) search(left,idxl-1,idxl,0);
if(idxl&&right>=idxl+1) search(idxl+1,right,idxl,1);
if(idxl==left) b[idxl][0]=0;
if(idxl==right) b[idxl][1]=0;
}
int main()
{
int T,n;
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
printf("Case %d:/n",i);
scanf("%d",&n);
int max=0,idx=0;
for(int j=1;j<=n;j++)
{
scanf("%d",&h[j]);
if(h[j]>max) max=h[j],idx=j;
}
if(idx-1>=1) search(1,idx-1,idx,0);
if(idx+1<=n) search(idx+1,n,idx,1);
if(idx==1) b[idx][0]=0;
if(idx==n) b[idx][1]=0;
for(int j=1;j<=n;j++)
printf("%d %d/n",b[j][0],b[j][1]);
}
return 0;
}