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.
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.
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.
12
12 0
81 2 1 3 1 2 1 5
41 22 43 05 0
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;}
- CF#204DIV2:B. Jeff and Periods
- B. Jeff and Periods
- B. Jeff and Periods
- CF#204DIV2:A. Jeff and Digits
- Codeforces Round #204 (Div. 2) B. Jeff and Periods
- Codeforces Round #204 (Div. 2) B - Jeff and Periods
- CodeForces #352B.Jeff and Periods
- cf 204 div2 C Jeff and Rounding 模拟
- cf 204 div2 D. Jeff and Furik 逆序对
- cf#204-div1 -D - Jeff and Removing Periods-莫队算法-维护等差数列
- Jeff and Periods
- CF 314B: Sereja and Periods
- CF 351D - Jeff and Removing Periods(离线 + 树状数组)
- CodeForce 204 Div2. C Jeff And Rounding
- Codeforces 351D:Jeff and Removing Periods
- CF#196DIV2:B-Xenia and Ringroad
- CF#215DIV2:B. Sereja and Suffixes
- CF 226 DIV2 B. Bear and Strings
- linux 消息队列
- 设计模式之装饰模式
- c语言数组--插入排序
- CF#204DIV2:A. Jeff and Digits
- Oracle DB RMAN 简单配置
- CF#204DIV2:B. Jeff and Periods
- QQ国际版汉化最简单方法
- tomcat内存溢出配置
- 1005
- ORACLE密码认证方式
- 禁止搜索引擎收录的方法
- iOS手势
- HUSTOJ problems
- Win7 od下send断点