Codeforces Round #204 (Div. 2)->B. Jeff and Periods

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 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.

Examples
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.

题意:给出有n个数的序列,如果x没有相同的数,就输出x和0,如果有多个x,输出它们坐标的差p[x],这个差是公差(common difference between numbers in the progression)(换句话说,相同的数(>2个)的坐标是等差数列即符合),不是公差的就算数字相同也不符合,比如样例2,1的下标是0 2 4 6,所以它们的p是2,2的下标是1 5,它们的p是4,然而比如6  1 2 2 2 1 1  这个样例,1的坐标是1,5,6,(5-1)!=(6-5),所以1不是,2的坐标的2,3,4,,(3-2)==(4-3),所以2是,即输出为1 2 1;

题解:设三个数组a[](记录下标),flag[](标记符合条件的数),p[](公差),cnt用来累加符合条件的数,然后就遍历,比较相同数坐标的差值是否是公差,好像没什么解释的了。。。

#include<bits/stdc++.h>
using namespace std;
int main() {
    int a[100001]={0}, flag[100001]={0}, p[100001]={0};
    int n, x, cnt = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        if(flag[x])
            continue;
        if(a[x]) {
            if(p[x] == 0)
                p[x] = i - a[x];
            else if(p[x] != i - a[x]) {
                flag[x] = 1;
                cnt--;
                
            }
    
        } else
            cnt++;
        a[x] = i;
        //cout<<i<<x<<cnt<<endl;
    }
        cout<<cnt<<endl;
        for(int i=1;i<=100000;i++){
            if(a[i]&&flag[i]==0)
                printf("%d %d
",i,p[i]);
        }
        return 0;
    }
原文地址:https://www.cnblogs.com/zhien-aa/p/6021284.html