zoukankan      html  css  js  c++  java
  • B. Jeff and Periods(cf)

    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.

    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 }
    View Code
  • 相关阅读:
    开源cms系统We7插件开发准备工作全面就绪 开源CMS
    We7促销力度惊人:又开源又送iphone! 开源CMS
    开源cms系统:We7 CMS 2.5版内测版发布啦! 开源CMS
    开源cms系统的发展趋势 开源CMS
    We7 CMS支撑全新宁夏检察院门户网站 开源CMS
    We7开放式维基文档中心正式开通 开源CMS
    我公司签约成都申达科技打造校园行业网站群建设新的应用平台 开源CMS
    西部动力中标北京京能热电股份有限公司班组管理系统网站群项目 开源CMS
    SQL查数据库表,字段
    DataTable Excel
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3352556.html
Copyright © 2011-2022 走看看