CodeForcesGym 100502H Clock Pictures

Clock Pictures

Time Limit: 1000ms
Memory Limit: 524288KB
This problem will be judged on CodeForcesGym. Original ID: 100502H
64-bit integer IO format: %I64d      Java class name: (Any)

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.

Task

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 ≤ 200000), the number of hands on the clock.

Each of the next two lines contains n integers ai (0 ≤ ai < 360000), 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 ai 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.

 

Figure H.1: Sample input 2

Sample Input 1                                                      Sample Output 1

6

1 2 3 4 5 6

7 6 5 4 3 1

impossible


Sample Input 2                                                      Sample Output 2

2

0 270000

180000 270000

possible

Sample Input 3                                                      Sample Output 3

7

140 130 110 120 125 100 105

235 205 215 220 225 200 240

impossible

NCPC 2014 Problem H: Clock Pictures

 

解题:直接用kmp...

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200010,mod = 360000;
 4 int fail[maxn],a[maxn],b[maxn],c[maxn<<1],n;
 5 void getNext(){
 6     fail[0] = fail[1] = 0;
 7     for(int i = 1; i < n; ++i){
 8         int j = fail[i];
 9         while(j && a[i] != a[j]) j = fail[j];
10         fail[i + 1] = a[i] == a[j]?j+1:0;
11     }
12 }
13 int main() {
14     while(~scanf("%d",&n)) {
15         for(int i = 0; i < n; ++i)
16             scanf("%d",a+i);
17         for(int i = 0; i < n; ++i)
18             scanf("%d",b+i);
19         sort(a,a+n);
20         sort(b,b+n);
21         c[n-1] = (b[0] - b[n - 1] + mod)%mod;
22         for(int i = 1; i < n; ++i){
23             a[i-1] = (a[i] - a[i-1] + mod)%mod;
24             b[i-1] = (b[i] - b[i-1] + mod)%mod;
25             c[i - 1] = c[i + n - 1] = b[i - 1];
26         }
27         getNext();
28         bool flag = false;
29         for(int i = 0,j = 0; i < (n<<1)-1; ++i){
30             while(j && a[j] != c[i]) j = fail[j];
31             if(a[j] == c[i]) j++;
32             if(j == n-1){
33                 flag = true;
34                 break;
35             }
36         }
37         puts(flag?"possible":"impossible");
38     }
39     return 0;
40 }
41 /*
42 6
43 1 2 3 4 5 6
44 7 6 5 4 3 1
45 
46 2
47 0 270000
48 180000 270000
49 
50 7
51 140 130 110 120 125 100 105
52 235 205 215 220 225 200 240
53 */
View Code

 

posted @ 2015-04-27 19:46  狂徒归来  阅读(445)  评论(0编辑  收藏  举报