• codeforces-Jeff and Periods


    Jeff and Periods

    Description

    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.

    Sample Input

    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
     
    题目大意:长度为n的序列a,x在序列a中,a中所有值为x的元素,如果下标构成等差数列,就输出这个元素的值和公差,否则不输出此元素。如果这个元素只出现了一次,则输出这个元素的值和0。在output的第一行输出一共有多少个下标能构成等差数列的元素。
     
    这个题目一开始没读懂,后来才明白是找值相同的数,它们的下标构成的等差数列的公差。
    (For that, he needs to find all values of x, 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.
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 #define maxn 100005
     5 
     6 int step[maxn]; 
     7 int first[maxn];
     8 int last[maxn];
     9 
    10 int main()
    11 {
    12     int i, n, a, c = 0;
    13     memset(first, -1, sizeof(first));
    14     memset(last, -1, sizeof(last));
    15     memset(step, 0, sizeof(step));
    16     while (scanf("%d", &n) != EOF)
    17     {
    18         for (i = 0; i < n; i++)
    19         {
    20             scanf("%d", &a);
    21             if (first[a] == -1)               //取a的值为数组下标,a为元素的值
    22             {
    23                 first[a] = i;                //first[a]为【第一次】出现 之前未出现的元素 的位置
    24                 c++;                        //只要是第一次出现此数,就计数
    25             }
    26             else 
    27             {
    28                 if (step[a] == 0)
    29                 {
    30                     step[a] = i - first[a];       //【第一次】出现 某个元素 据第一个与它值相同的元素的步数(即公差)
    31                 }
    32                 else if (step[a] > 0 && (i - last[a]) != step[a])        //如果之后出现的元素 据 上一个与它相同的元素的步数 != 公差
    33                 {
    34                     step[a] = -1;                                    //则置为-1,不输出此元素
    35                     c--;                                            //不计入此数
    36                 }
    37             }
    38             last[a] = i;                                   //记录这次该元素的位置,以便下次此值再出现时比较步数与公差
    39         }
    40         printf("%d
    ", c);
    41         for (i = 1; i <= maxn - 1; i++)
    42         {
    43             if (first[i] >= 0)
    44             {
    45                 if (step[i] >= 0)                          //对于只出现一次的数,step[i] = 0,也会输出
    46                 {
    47                     printf("%d %d
    ", i, step[i]);          //i为元素a的值,step[i]为公差
    48                 }
    49             }
    50         }
    51     }
    52     return 0;
    53 }

     

     

  • 相关阅读:
    3.默认参数的注意事项
    2.关键字global,nonlocal
    1.函数的结构,调用,传参,形参,实参,args,kwargs,名称空间,高阶函数
    19. 实现一个整数加法计算器(多个数相加):
    20.文件操作
    18.使用for循环计算+1-3+5-7+9-11+13...99的结果
    ubuntu docker更新镜像源
    虚拟环境的使用
    前端基础之HTML
    jQuery基本使用
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3380911.html
Copyright © 2020-2023  润新知