cf B. Jeff and Periods

http://codeforces.com/contest/352/problem/B

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 int a[100010];
 6 int n;
 7 bool vis[100010];
 8 int len;
 9 struct node
10 {
11     int x,id;
12     bool operator <(const node &a)const
13     {
14         return (x<a.x)||(x==a.x&&id<a.id);
15     }
16 } p[100010],p1[100010];
17 int main()
18 {
19     while(scanf("%d",&n)!=EOF)
20     {
21         memset(vis,0,sizeof(vis));
22         int cnt=0;
23         for(int i=1; i<=n; i++)
24         {
25             scanf("%d",&a[i]);
26             p[cnt].x=a[i];
27             p[cnt++].id=i;
28         }
29         sort(p,p+cnt);
30         bool flag1=false;
31         int cnt1=0;
32         bool flag2=false;
33         bool flag3=false;
34         for(int i=0; i<cnt; i++)
35         {
36             if(!vis[p[i].x])
37             {
38                 if(!flag1)
39                 {
40                     flag1=true;
41                 }
42                 else
43                 {
44                     if(!flag2)
45                     {
46                         p1[cnt1].x=p[i-1].x;
47                         p1[cnt1++].id=len;
48                     }
49                 }
50                 flag2=false;
51                 flag3=false;
52                 len=0;
53                 vis[p[i].x]=true;
54             }
55             else
56             {
57                 if(flag2)continue;
58                 if(!flag3)
59                 {
60                     len=p[i].id-p[i-1].id;
61                     flag3=true;
62                 }
63                 else
64                 {
65                      if(p[i].id-p[i-1].id!=len)
66                      {
67                          flag2=true;
68                      }
69                 }
70             }
71         }
72         if(!flag2)
73         {
74             p1[cnt1].x=p[cnt-1].x;
75             p1[cnt1++].id=len;
76         }
77         printf("%d
",cnt1);
78         for(int i=0; i<cnt1; i++)
79         {
80             printf("%d %d
",p1[i].x,p1[i].id);
81         }
82     }
83     return 0;
84 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3928946.html