679-dropping Balls
Time limit:3.000 seconds
Topic Link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& category=&problem=620&mosmsg=submission+received+with+id+17772253
A number of K balls is dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down either follows the path of the left subtree, or follows the path of the right subtree, until it Stops at one of the leaf nodes of FBT. To determine a ball ' s moving direction a flag was set up in every Non-terminal node with the values, either false or true. Initially, all of the flags is false. When visiting a non-terminal node if the flag ' s current value at this node is false and then the ball would first switch this Flag ' s value, i.e, from the false to the true, and then follow the left subtree of this node to keep moving down. Otherwise, it'll also switch this flag's value, i.e, from the true to the false, but would follow the right subtree of t He node to keep moving down. Furthermore, all nodes of FBT is sequentially numbered, starting at 1 with nodes on depth 1, and then those on depth 2, a nd So on. Nodes on any depth is numbered from left to right.
example, Fig. 1 represents a fully binary tree of maximum depth 4 with the node Nu Mbers 1, 2, 3, ..., 15. Since all of the flags is initially set to is false, the first ball being dropped would switch flag ' s values at node 1, no De 2, and Node 4 before it finally stops at position 8. The second ball being dropped would switch flag ' s values at node 1, node 3, and Node 6, and stop at position 12. Obviously, the third ball being dropped would switch flag ' s values at node 1, node 2, and node 5 before it stops at Positio N.
Now consider a number of test cases where the values would be given for each test. The first value is D, the maximum depth of FBT, and the second one are I, the i-th ball being dropped. You may assume the value of I won't exceed the total number of the leaf nodes for the given FBT. Determine the stop position P for each test case. For each test cases the range of parameters D and I are as below:
2≤d≤20, and 1≤i≤524288.
Input
Contains L + 2 lines.
Line 1 L The number of test cases
Line 2 D1 I1 test Case #1, both decimal numbers that is separated by one blank
...
Line K + 1 Dk Ik test Case #k
Line L + 1 Dl-Il test Case #l
Line L + 2-1 a constant '-1 ' representing the end of the input file
Output
Contains l lines.
Line 1, the stop position P for the test case #1
...
Line k The stop position P for the test case #k
...
Line L The Stop position P for the test case #l
Sample Input
5
4 2
3 4
10 1
2 2
8 128
-1
Sample Output
12
7
512
3
255
The easiest to think of, a violent mock-up, and then, sure enough, timed out
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 #defineMaxd 207 ints[1<< -];8 intMain ()9 {Ten intD,i,n; OneCIN >>N; A while(n--) - { -scanf"%d%d",&d,&I); thememset (s),0,sizeof(s)); - intK; - for(inti =0; i < I; i++) - { +K =1; - for(intj =0; J < D; J + +) + { A if(S[k]) at { - //cout << k << Endl; -S[K] =0; -K = (k<<1) +1; - } - Else in { - //cout << k << Endl; toS[K] =1; +K = k<<1; - } the } * } $cout << k/2<<Endl;Panax Notoginseng } - return 0; the}
Then, a different way of thinking, directly see the first ball, when the ball is the first I fall at this point of the small ball, if I is odd, it is left to the first (i+1)/2 small ball, I is even, it is the right to go to the first I/2 ball, so that you can directly simulate the path of the last ball.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 #defineMaxd 207 ints[1<< -];8 intMain ()9 {Ten intD,i,n; OneCIN >>N; A while(n--) - { -CIN >> D >>I; the intK =1; - for(inti =0; I < D1; i++) - { - if(i%2) + { -K = k<<1; +I = (i+1)/2; A } at Else - { -K = (k<<1) +1; -I = i/2; - } - } incout << k <<Endl; - } to return 0; +}
Uva679-dropping Balls