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 hasNDifferent positive integers, and each number is not greaterN. 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 numberA1FromNIntegers, and Bob will write it down on the first paper, that'sB1. Then in the followingKTh rounds, Alice will choose a numberAK(2 ≤ k ≤ n), Then Bob will write the numberBK= |AK-Bk-1| OnKTh paper. |X| Means the absolute valueX.
Now Alice and Bob want to kown, what is the maximum and minimum valueBN. And you shoshould tell them how to achieve that!
Input
The input consists of multiple test cases;
For each test case, the first line consists one integerN, 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 containingNNumbers, the order of integers that Alice shoshould choose to achieve the minimum value. Then print one line containingNNumbers, the order of integers that Alice shoshould choose to achieve the maximum value.
Attention: Alice won't choose a integer more than twice.
Sample Input
2
Sample output
1 11 22 1
Question: Find the sequence to minimize and maximize B [N;
It is easy to find that the minimum value is 0, and the maximum value is n-1;
And because this is a special question, 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;}
I have been struggling with the absolute value during the competition, so I didn't think about it well... It's really a failure!
Come on !!!
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 shoshould 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 1N. 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 integerNIndicates the number of the machines (2 ≤ n ≤10000). The following line will includeN-1Numbers.I-Th numberAIMeans that the output of MachineI + 1Will be the input of MachineAI(AI≤I). The same Code will be appearedAt 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
31 171 1 2 2 3 3
Sample output
23
Hint
Case 1 is the example.
Case 2:
This problem contains massive input and output, please use efficient Io methods.
That is to say, there are two kinds of pipes. The height of the tube can be infinitely long, but the width is the same. Ask the smallest width;Enter the number of I to indicate the container to which the I + 1 container is to be input;
Thought: Actually, it is DFS. Every time the DFS goes down, save the subtree width and find the maximum value. If there are multiple values, it is the maximum value + CNT width;
# 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]; // take the large stream 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]);}
I didn't make it during the competition. The reason for WA is that it should be big when looking for branches;
Come on !!!
Zoj monthly, August 2014