B. Jeff and Periods

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Jeff got hold of an integer sequence a1a2...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 a1a2...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
Note

In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.


解题说明:此题要求找出数列中下标为等差数列的那些数(相同的数多次出现),输出下标的等差值。做法是记录每一个数当前出现的位置,计算它与上一次出现的位置的距离,然后判断此距离是否和上一次和上上次出现的距离相等。如果有一次不等,那么这个数就不能构成等差数列,也就不输出该数。注意如果某个数只出现一次,那么也算是等差数列。

#include 
#include
#include
#include
#include
#include
using namespace std;

int a[100000];
int first[100001];
int step[100001];
int last[100001];

int main()
{
	int i,n,a,c=0;
	memset(first,-1,sizeof(first));
	memset(last,-1,sizeof(last));
	memset(step,0,sizeof(step));
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		scanf("%d", &a);
		if(first[a] == -1)
		{
			first[a] = i;
			c++;
		}
		else
		{
			if(step[a] == 0)
			{
				step[a] = i - first[a];
			}
			else if(step[a] > 0 && (i - last[a]) != step[a])
			{
				step[a] = -1;
				c--;
			}
		}
		last[a] = i;
	}
	printf("%d\n", c);
	for(i = 1; i <= 100000; i++)
	{	
		if(first[i] >= 0)
		{
			if(step[i] >= 0)
			{
				printf("%d %d\n", i, step[i]);
			}
		}
	}
	return 0;
}



你可能感兴趣的:(AC路漫漫)