H. Elven Postman
Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something to be taken lightly. Also, they live on trees. However, there is something about them you may not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more “traditional” methods.
So, as a elven postman, it is crucial to understand how to deliver the mail to the correct room of the tree. The elven tree always branches into no more than two paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only that, when numbering the rooms, they always number the room number from the east-most position to the west. For rooms in the east are usually more preferable and more expensive due to they having the privilege to see the sunrise, which matters a lot in elven culture.
Anyways, the elves usually wrote down all the rooms in a sequence at the root of the tree so that the postman may know how to deliver the mail. The sequence is written as follows, it will go straight to visit the east-most room and write down every room it encountered along the way. After the first room is reached, it will then go to the next unvisited east-most room, writing down every unvisited room on the way as well until all rooms are visited.
Your task is to determine how to reach a certain room given the sequence written on the root.
For instance, the sequence 2,\ 1,\ 4,\ 32, 1, 4, 3 would be written on the root of the following tree.
Input Format
First you are given an integer T(T\le 10)T(T≤10) indicating the number of test cases.
For each test case, there is a number n(n\le 1000)n(n≤1000) on a line representing the number of rooms in this tree. nnintegers representing the sequence written at the root follow, respectively a_1,\ ...,\ a_na1, ..., an where a_1,\ ...,\ a_n\in \{1,\ ...,\ n\}a1, ..., an∈{1, ..., n}.
On the next line, there is a number qq representing the number of mails to be sent.
After that, there will be qq integers x_1,\ ...,\ x_qx1, ..., xq indicating the destination room number of each mail.
Output Format
For each query, output a sequence of move (EE or WW) the postman needs to make to deliver the mail. For that EEmeans that the postman should move up the eastern branch and WW the western one. If the destination is on the root, just output a blank line would suffice.
Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
样例输入
2
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
样例输出
E
WE
EEEEE
题目来源
ACM-ICPC 2015 Changchun Preliminary Contest
题意:倒过来看是一个二叉排序树,在查找过程中,往树的左边走是W,往树的右边走是E
所以根据题目输入的数据,建树然后再查找就好了,在查找的过程中输出W或者E,当查找的元素为树根的时候,就直接输出一个空就可以了。
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int n,q;
int a,b;
typedef struct node
{
int d;
struct node *left,*right;
}BiNode,*BiTree;
BiTree build(BiTree &T,int key)
{
if(T==NULL)
{
T=(BiTree)malloc(sizeof(BiNode));
T->d=key;
T->left=NULL;
T->right=NULL;
}
else
{
if(T->d<key)
T->left=build(T->left,key);
else
T->right=build(T->right,key);
}
return T;
}
void find(BiTree &T,int key)
{
if(T)
{
if(T->d==key)
{
printf("\n") ;
return ;
}
else
{
if(T->d<key)
{
printf("W");
find(T->left,key);
}
else
{
printf("E");
find(T->right,key);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
BiTree T;
T=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
T=build(T,a);
}
scanf("%d",&q);
for(int i=0;i<q;i++)
{
scanf("%d",&b);
if(T==NULL && T->d==b)
printf(" \n");
else
find(T,b);
}
}
return 0;
}
注意:
1.查找过程中,注意段错误,就是有不合法的指针,我在这个地方出现的错误是没有判断树为空时这一种情况
2.建树的过程要规范,结构体不要随意改变,每一个建树都用这一模板就好了,随意改动,写代码的时候,自己把自己写糊涂了。
3.要熟悉树的知识,再做这个题目的时候,老师出现段错误,总是改不出来,起始就是查找的时候出现错误,没有对树为空的时候这一情况进行判断,所以又翻看了之前的代码。
G. The Water Problem
In Land waterless, water is a very limited resource. People always fight for the biggest source of water.
Given a sequence of water sources with a_1,\ a_2,\ a_3,\ ...,\ a_na1, a2, a3, ..., an representing the size of the water source.
Given a set of queries each containing 22 integers ll and rr, please find out the biggest water source between a_laland a_rar.
Input Format
First you are given an integer T(T\le 10)T(T≤10) indicating the number of test cases.
For each test case, there is a number n(0 \le n \le 1000)n(0≤n≤1000) on a line representing the number of water sources.
nn integers follow, respectively a_1,\ a_2,\ a_3,\ ...,\ a_na1, a2, a3, ..., an, and each integer is in \{1,\ ...,\ 10^6\}{1, ..., 106}.
On the next line, there is a number q(0 \le q \le 1000)q(0≤q≤1000) representing the number of queries.
After that, there will be qq lines with two integers ll and r(1 \le l \le r \le n)r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source.
Output Format
For each query, output an integer representing the size of the biggest water source.
样例输入
3
1
100
1
1 1
5
1 2 3 4 5
5
1 2
1 3
2 4
3 4
3 5
3
1 999999 1
4
1 1
1 2
2 3
3 3
样例输出
100
2
3
4
4
5
1
999999
999999
1
题目来源
ACM-ICPC 2015 Changchun Preliminary Contest
题意:再输入的数据中,查找区间内最大值,是一道很简单的题目。
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn =1e3+10;
int t,n;
int a[maxn];
int k,l,r;
int main()
{
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&k);
for(int i=0;i<k;i++)
{
scanf("%d%d",&l,&r);
int max=a[l];
for(int j=l+1;j<=r;j++)
{
if(a[j]>max)
{
max=a[j];
}
}
printf("%d\n",max);
}
}
}
return 0;
}
注意:
1.看清楚有一个变量t,来控制输入多少组数据,简单题敲完代码,也要快中求稳。