CF#204DIV2:B. Jeff and Periods

来源:互联网 发布:菩提迦耶 知乎 编辑:程序博客网 时间:2024/06/05 15:28

One day Jeff got hold of an integer sequence a1,a2, ..., an of lengthn. The boy immediately decided to analyze the sequence. For that, he needs to find all values ofx, for which these conditions hold:

  • x occurs in sequence a.
  • Consider all positions of numbers x in the sequencea (such i, thatai = 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 integersa1, a2, ...,an(1 ≤ ai ≤ 105). The numbers are separated by spaces.

Output

In the first line print integer t — the number of validx. On each of the next t lines print two integers x andpx, wherex is current suitable value, px is the common difference between numbers in the progression (ifx occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasingx.

Sample test(s)
Input
12
Output
12 0
Input
81 2 1 3 1 2 1 5
Output
41 22 43 05 0
Note

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


 

这道题看了我好久才弄懂意思,就是要找出数列中所有下标为等差数列的那些数输出,并输出下标的等差值

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int hash[100005];struct node{    int d;    int pre;    int is;} s[100005];int main(){    int n,i,a,r,cnt,maxn;    while(~scanf("%d",&n))    {        memset(hash,0,sizeof(hash));        memset(s,0,sizeof(s));        maxn = 0;        for(i = 0; i<n; i++)        {            scanf("%d",&a);            maxn = max(maxn,a);//找出最大的那项            if(!hash[a])            {                s[a].pre = i;//将现在的位置                hash[a]++;                s[a].is = 0;            }            else if(hash[a] == 1)            {                r = i - s[a].pre;//计算位置的公差                s[a].d = r;//放入公差                s[a].pre = i;//位置放入                hash[a]++;            }            else if(s[a].is != -1)            {                hash[a]++;                r = i-s[a].pre;                s[a].pre = i;                if(r!=s[a].d)                    s[a].is = -1;//公差不相等,标记为不行            }        }        cnt = 0;        for(i = 1; i<=maxn; i++)        {            if(s[i].is==0 && hash[i])//所有符合的状况                cnt++;        }        printf("%d\n",cnt);        for(i = 1; i<=maxn; i++)        {            if(s[i].is == -1 || !hash[i])                continue;            printf("%d %d\n",i,s[i].d);        }    }    return 0;}


 

原创粉丝点击