B. Jeff and Periods
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputOne day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold:
- x occurs in sequence a.
- Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression.
Help Jeff, find all x that meet the problem conditions.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 105). The numbers are separated by spaces.
Output
In the first line print integer t — the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x.
Sample test(s)
Input
1
2
Output
1
2 0
Input
8
1 2 1 3 1 2 1 5
Output
4
1 2
2 4
3 0
5 0
1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #include <algorithm> 5 const int Max=100005; 6 using namespace std; 7 int main() 8 { 9 int n,x; 10 int vis[Max],p[Max]; 11 while(~scanf("%d",&n)) 12 { 13 int k = 0,i,j; 14 vector<int>G[Max]; 15 memset(vis,0,sizeof(vis)); 16 for (i = 0; i < n; i++) 17 { 18 scanf("%d",&x); 19 G[x].push_back(i);//将所有x的位置存入vector中 20 if (!vis[x]) 21 { 22 vis[x] = 1; 23 p[k++] = x; 24 } 25 26 } 27 int cnt = 0; 28 memset(vis,-1,sizeof(vis)); 29 for (j = 0; j < k; j++) 30 { 31 int len = G[p[j]].size(); 32 if (len==1) 33 { 34 ++cnt; 35 vis[p[j]]= 0; 36 continue; 37 } 38 int d = G[p[j]][1]-G[p[j]][0];//求公差 39 for (i = 1; i < len; i++) 40 { 41 if (G[p[j]][i]-G[p[j]][i-1]!=d) 42 break; 43 } 44 if (i >= len)//说明p[j]的各位置是等差数列 45 { 46 ++cnt; 47 vis[p[j]] = d;//表示p[j]各位置的公差为d 48 } 49 } 50 printf("%d ",cnt); 51 sort(p, p+k); 52 for (i = 0; i < k ; i++) 53 { 54 if (vis[p[i]]!=-1) 55 { 56 printf("%d %d ",p[i],vis[p[i]]); 57 } 58 } 59 } 60 return 0; 61 }