Sicily周赛 Clock Pictures

来源:互联网 发布:黑米软件怎么样 编辑:程序博客网 时间:2024/05/18 00:25
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 ai (0 <= ai < 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 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.

Sample Input
Copy sample input to clipboard
样例一:61 2 3 4 5 67 6 5 4 3 1样例二:20 270000180000 270000样例三:7140 130 110 120 125 100 105235 205 215 220 225 200 240
Sample Output
样例一:impossible样例二:possible样例三:impossible
Hint

 Here is the figure for the second sample input:



题解:

把间隔找出来之后,判断两个间隔数组能否通过循环移位得到另一个

比如 s1 = "abcd" s2 = "dabc" 

则移动d到前面就可以了

考虑到时间问题

这里用 s1 + s1 和 s2 去匹配

判断s2是否在s1 + s1 中匹配就可以了

用KMP算法


#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <cmath>#include <algorithm>using namespace std;int arr[200005] ;int brr[200005] ;int d1[200005],d2[200005];int Source[200005*2];int _next[200005];void getNext(const int* T,int n){memset(_next , 0 , sizeof(_next));_next[0] = -1 ;int len = n;int i = 0,j = -1 ;  // j 前缀, i 后缀while(i < len){if(-1 == j || T[i] == T[j]){i++,j++;//_next[i] = j ;if(T[i] != T[j])   //对上一句的优化 {_next[i] = j ;}else{_next[i] = _next[j];} }else{j = _next[j];}} }bool KMP(const int* s ,const int* T,int n){int i = 0,j = 0 ;  //i 为原串指针, j 为模式串指针int len = 2*n;int limit = n;int ans = 0 ;while(i < len){if(-1 == j || s[i] == T[j]){i++,j++;}else{j = _next[j];}if(j == limit){return true;}} return false ;}bool check(int n){sort(arr , arr + n);sort(brr , brr + n);int cnt = 1;memset(d1,0,sizeof(d1));memset(d2,0,sizeof(d2));d1[0] = (360000 - arr[n-1]) + arr[0];d2[0] = (360000 - brr[n-1]) + brr[0];for(int i = 1;i < n;i++,cnt++){d1[cnt] = arr[i] - arr[i-1];d2[cnt] = brr[i] - brr[i-1];}getNext(d2 , cnt);for(int i = 0;i < 2*cnt;i++)Source[i] = d1[i%cnt];if(KMP(Source , d2 , cnt))return true;else return false;}int main(){int n ;while(~scanf("%d",&n)){for(int i = 0;i < n;i++)scanf("%d",arr+i);for(int i = 0;i < n;i++)scanf("%d",brr+i);if(check(n))puts("possible");elseputs("impossible");}}

这个方法肯定不是最好的,有什么好方法麻烦指点哈~~

0 0
原创粉丝点击