当前位置:网站首页>POJ1988_ Cube Stacking
POJ1988_ Cube Stacking
2022-07-27 12:21:00 【51CTO】
Cube Stacking
Time Limit: 2000MS | | Memory Limit: 30000K |
Total Submissions: 18973 | | Accepted: 6605 |
Case Time Limit: 1000MS | ||
Description
Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:
moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.
Write a program that can verify the results of the game.
Input
* Line 1: A single integer, P
* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X.
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.
Output
Print the output from each of the count operations in the same order as the input file.
Sample Input
6 M 1 6 C 1 M 2 4 M 2 6 C 3 C 4
Sample Output
1 0 2
Source
The main idea of the topic :
Yes N(N<=30,000) Pile block , At first, every pile is a square . Block number 1 – N. There are two operations :
- M x y : Means to put a square x On the heap , Pick it up and fold it y On the pile .
- C x : Ask the square x How many squares are there below .
At most P (P<=100,000) Time . Yes, every time C operation , Output results .
Ideas :
Use and look up sets , Add every two bricks , Let the brick below be the parent node , The brick on the top serves as the root node ; When two piles of bricks merge , Let the root node of the lower heap be the root node of the upper heap . Among them , except father Out of array , You also need two arrays sum( It is used to record how many bricks there are in each pile ),under Array ( Used to record the i How many bricks are there under a brick ), among sum The array is only updated when the heap is merged ,under The array should be updated when merging the heap and when compressing the path .
Code :
边栏推荐
- 20210518-Cuda
- Solve the problem of @onetomany query falling into circular reference
- Sword finger offer note: t45. arrange the array into the smallest number
- 2021-3-22-tencent - minimum number of guards
- NewTicker使用
- Fundamentals of mathematics 01
- JS parasitic combinatorial inheritance
- STS download tutorial (the solution cannot be downloaded on the include official website)
- Chapter 10 enumeration classes and annotations
- shell中的while循环实例
猜你喜欢

Watermelon book chapter 3 (first & second)

图像分割 vs Adobephotoshop(PS)

Chapter 12 generics

解决方案:Can not issue executeUpdate() or executeLargeUpdate() for SELECTs

B 站 713 事故后的多活容灾建设|TakinTalks 大咖分享

Alibaba cloud RDS exception during pool initialization
Ali II: what if the AOF file in redis is too large?

Advance in the flutter project_ image_ Picker component usage

2021-3-22-directed graph sorting

评价自动化测试优劣的隐性指标
随机推荐
go入门篇 (5)
Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
广东财政多举措助力稳住粮食安全“压舱石”
Detailed explanation of deeplab series (simple and practical annual summary)
NewTicker使用
Idea: can't use subversion command line client: SVN solution
Current situation and development trend of accounting computerization
Wechat applet must use interface "suggestions collection"
The chess robot "broke" the chess boy's finger...
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
二分查找判定树(二分查找树平均查找长度)
Leetcode 02: sword finger offer 58 - I. flip the word order (simple); T123. Verify palindrome string; T9. Palindromes
JS-寄生组合式继承
In the first half of the year, the number of fires decreased by 27.7%. Guangdong will improve the fire safety quality of the whole people in this way
Solution: can not issue executeupdate() or executelargeupdate() for selections
compute_ class_ weight() takes 1 positional argument but 3 were given
While loop instance in shell
2021-3-19-byte-face value
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
Go replace with local code