按照艾神的推荐,在vjudge上刷这个章节的题。
A - River Hopscotch
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, Lunits away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
分析:
二分答案,二分最小的跳跃距离,判断是否符合题意,在继续二分。
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int l,N,m;
int a[50010];
int check(int n)
{
int cnt= 0;
int j=0;
for(int i=1;i<=N+1;i++)
{
if(a[i]-a[j]<n) cnt++;
else j=i;
}
if(cnt>m) return 0;
else return 1;
}
int binary()
{
int left = 0, r = l,ans=0;
while(left <= r)
{
int mid = (left + r) >> 1;
if(check(mid)==1) ans=mid,left = mid + 1;
else r = mid - 1;
}
return ans;
}
int main()
{
scanf("%d%d%d",&l,&N,&m);
for(int i=1;i<=N;i++)
scanf("%d",&a[i]);
a[0]=0,a[N+1]=l;
sort(a,a+N+1);
int ans=binary();
printf("%d\n",ans);
return 0;
}
B - Tree Recovery
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
分析:
前序遍历的第一个结点是根结点,对于中序遍历,找到根结点,其中左半部分为其左子树,右半部分为其右子树,可知通过递归即可实现。
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
struct node{
char c;
struct node *left,*right;
};
node *creat(char *pre,char *in,int len)
{
if(len<=0) return NULL;
node *root=(node*)malloc(sizeof(node));
root->c=*pre;
char *p;
for(p=in;p!=NULL;p++)
{
if(*p==*pre)
break;
}
int k=p-in;
root->left=creat(pre+1,in,k);
root->right=creat(pre+k+1,p+1,len-k-1);
return root;
}
void print(node *root)
{
if(root==NULL)
return ;
print(root->left);
print(root->right);
printf("%c",root->c);
}
int main()
{
char pre[30],in[30];
node *root;
root = (node*)malloc(sizeof(node));
while(cin>>pre>>in)
{
int len=strlen(pre);
node *root=creat(pre,in,len);
print(root);
printf("\n");
}
return 0;
}