Clock Pictures
Clock Pictures
题目描述
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.
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.
输入
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.
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 one line containing one word: possible if the clocks could be showing the same time,impossible otherwise.
![](http://exam.upc.edu.cn/upload/image/20161026/20161026141950_83985.jpg)
样例输入
6
1 2 3 4 5 6
7 6 5 4 3 1
样例输出
impossible
分析:最小表示法或KMP;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <ctime> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") #define freopen freopen("in.txt","r",stdin) const int maxn=2e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,k,t,len,a[maxn],b[maxn]; int getMin(int len,int str[]) { int i = 0, j = 1; int l; while (i < len && j < len) { for (l = 0; l < len; l++) if (str[(i + l) % len] != str[(j + l) % len]) break; if (l >= len) break; if (str[(i + l) % len] > str[(j + l) % len]) { if (i + l + 1 > j) i = i + l + 1; else i = j + 1; } else if (j + l + 1 > i) j = j + l + 1; else j = i + 1; } return i < j ? i : j; } int main() { int i,j; scanf("%d",&n); rep(i,0,n-1)scanf("%d",&a[i]); rep(i,0,n-1)scanf("%d",&b[i]); sort(a,a+n); sort(b,b+n); a[n]=a[0],b[n]=b[0]; rep(i,0,n-1) { a[i]=a[i+1]-a[i]; b[i]=b[i+1]-b[i]; if(i==n-1)a[i]+=360000,b[i]+=360000; } int pos1=getMin(n,a),pos2=getMin(n,b); for(int i=pos1,j=pos2,cnt=0;cnt<n;cnt++,i++,j++) { if(i==n)i=0; if(j==n)j=0; if(a[i]!=b[j])return 0*puts("impossible"); } puts("possible"); //system("Pause"); return 0; }