【CSU-1581】解题报告(思维,KMP)
原始题目
Problem H: Clock Pictures
1581: Clock Pictures
- Time Limit: 3 Sec
- Memory Limit: 128 Mb
- Submitted: 322
- Solved: 82
Description
You have two pictures of an unusual kind of clock. The clock has n hands, each having the same length and no kind of marking whatsoever. Also, the numbers on the clock are so faded that you can’t even tell anymore what direction is up in the picture. So the only thing that you see on the pictures, are n shades of the n hands, and nothing else.
You’d like to know if both images might have been taken at exactly the same time of the day, possibly with the camera rotated at different angles.
Given the description of the two images, determine whether it is possible that these two pictures could be showing the same clock displaying the same time.
Input
The first line contains a single integer \(n\) \((2 ≤ n ≤ 200 000)\), the number of hands on the clock.
Each of the next two lines contains n integers \(a\_i\) \((0 ≤ a\_i < 360 000)\), representing the angles of the hands of the clock on one of the images, in thousandths of a degree.
The first line represents the position of the hands on the first image, whereas the second line corresponds to the second image. The number \(a\_i\) denotes the angle between the recorded position of some hand and the upward direction in the image, measured clockwise. Angles of the same clock are distinct and are not given in any specific order.
Output
Output one line containing one word: possible if the clocks could be showing the same time,impossible otherwise.
Sample Input
6
1 2 3 4 5 6
7 6 5 4 3 1
Sample Output
impossible
HINT
题目大意
- 一个有\(n\)个指针的表盘,看不清数字,以随机顺序给出n个指针相对记录位置(不固定)的顺时针旋转角度,问两个表盘经过旋转是否能够重合(是否表示一个表盘)。
解题思路
- 对于相同表盘,记录位置不固定,但是两个指针间的角度差固定,判断位移后可不可以相同。
- 注意第一个指针和末尾指针的角度差需要补3600000
- 将一组倍长做KMP可以解决这个问题,若位移后可以相同,那么倍长后2倍距离必包含另一串。
解题代码
1 |
|
收获与反思
- 仔细读题,分析题目
- 倍长KMP可以判断自身位移的情况。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!