Abs Problem
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
Alice and Bob is playing a game, and this time the game is all about the absolute value!
Alice has N different positive integers, and each number is not greater than N. Bob has a lot of blank paper, and he is responsible for the calculation things. The rule of game is pretty simple. First, Alice chooses a number a1 from the N integers, and Bob will write it down on the first paper, that‘s b1. Then in the following kth rounds, Alice will choose a number ak (2 ≤ k ≤ N), then Bob will write the number bk=|ak-bk-1| on the kth paper. |x| means the absolute value of x.
Now Alice and Bob want to kown, what is the maximum and minimum value of bN. And you should tell them how to achieve that!
Input
The input consists of multiple test cases;
For each test case, the first line consists one integer N, the number of integers Alice have. (1 ≤ N ≤ 50000)
Output
For each test case, firstly print one line containing two numbers, the first one is the minimum value, and the second is the maximum value.
Then print one line containing N numbers, the order of integers that Alice should choose to achieve the minimum value. Then print one line containing N numbers, the order of integers that Alice should choose to achieve the maximum value.
Attention: Alice won‘t choose a integer more than twice.
Sample Input
2
Sample Output
1 1 1 2 2 1 题意:找出序列使b[n]最小和最大;很容易发现最小值不是1就是0,最大值不是n就是n - 1;又由于这是特殊判断题,So。。。
#include<stdio.h> #include<math.h> int main() { int N,i,j,max,min; while(scanf("%d",&N)!=EOF) { min=N%4; max=(N-1)%4; if(min==0||min==3) min=0; else min=1; if(max==0||max==3) max=0; else max=1; printf("%d %d\n",min,N-max); printf("%d",N); for(i=N-1; i>0; i--) printf(" %d",i); printf("\n"); printf("%d",N-1); for(i=N-2; i>0; i--) printf(" %d",i); printf(" %d\n",N); } return 0; }
比赛的时候一直纠结与绝对值,没好好思考。。。真是太失败了!
加油!!!
Machine
Time Limit: 2 Seconds Memory Limit: 65536 KB
In a typical assembly line, machines are connected one by one. The first machine‘s output product will be the second machine‘s raw material. To simplify the problem, we put all machines into a two-dimension shelf. Every machine occupied exactly one grid and has two input ports and only one output port. One input port can get material from only one machine.
Pipes will be used to connect between these machines. There are two kinds of pipes : ‘I‘ kind and ‘L‘ kind. We should notice that the ‘I‘ kind pipe can be linked one by one. Each pipe will also occupied one grid.
In Bob‘s factory, each machine will get raw materials from zero, one or two other machines. Some machines don‘t need any input materials, but any machine must have an output. Machines are coded by numbers from 1 to n. The output of the machines with greater code can be the input of the machines with less code. The machine NO.1‘s output product will be the final product, and will not be any other machine‘s input. Bob‘s factory has a shelf with infinite height, but finite width. He will give you the dependency relationship of these machines, and want you to arrange these machines and pipes so that he can minimize the width of the shelf.
Here‘s an example for you to help understand :
Products will falling from higher machine to lower machine through the pipes. Here, machine 1 gets materials from machine 2 and machine 3. The whole width of this system is 2.
Input
For each case, the first line will be an integer n indicates the number of the machines (2≤ n≤ 10000). The following line will include n-1 numbers. The i-th number ai means that the output of machine i+1 will be the input of machine ai (ai≤ i). The same code will be appeared at most twice. Notice machine 1‘s output will be the final output, and won‘t be any machine‘s input.
Output
For each case, we need exactly one integer as output, which is the minimal width of the shelf.
Sample Input
3 1 1 7 1 1 2 2 3 3
Sample Output
2 3
Hint
Case 1 is the example.
Case 2:
This problem contains massive input and output, please use efficient IO
methods.
题意:就是说有2种管子,管子的高度可以无限长,但宽度一致,问最小的宽度;输入第i个数表示第i+1个容器将要输向哪个容器;
思路:其实就是dfs,每次dfs下去,把子树宽度保存下来,然后找最大值,如果有多个,就是最大值+cnt宽度;
#include<stdio.h> #include<string.h> #include<vector> using namespace std; vector<int>map[10005]; int dp[20005]; void dfs(int p) { int i,j,flag,len; len=map[p].size(); dp[p]=0; for(i=0;i<len;i++) { j=map[p][i]; dfs(j); if(dp[p]<dp[j])flag=dp[j];//取大流 else if(dp[p]==dp[j])flag=dp[p]+1; dp[p]=flag; } if(dp[p]==0)dp[p]=1; } int main() { int n,a; while(scanf("%d",&n)>0) { for(int i=0;i<=n;i++)map[i].clear(); for(int i=2;i<=n;i++) { scanf("%d",&a); map[a].push_back(i); } dfs(1); printf("%d\n",dp[1]); } }
比赛的时候也没做出来,总是WA原因是找分支的时候应该取大的;
加油!!!