codeforces-Jeff and Periods
2013-10-21 17:12
288 查看
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的第一行输出一共有多少个下标能构成等差数列的元素。
这个题目一开始没读懂,后来才明白是找值相同的数,它们的下标构成的等差数列的公差。
[b](For that, he needs to find all values of x, [/b]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.)
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的第一行输出一共有多少个下标能构成等差数列的元素。
这个题目一开始没读懂,后来才明白是找值相同的数,它们的下标构成的等差数列的公差。
[b](For that, he needs to find all values of x, [/b]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.)
#include<stdio.h> #include<string.h> #define maxn 100005 int step[maxn]; int first[maxn]; int last[maxn]; int main() { int i, n, a, c = 0; memset(first, -1, sizeof(first)); memset(last, -1, sizeof(last)); memset(step, 0, sizeof(step)); while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) { scanf("%d", &a); if (first[a] == -1) //取a的值为数组下标,a为元素的值 { first[a] = i; //first[a]为【第一次】出现 之前未出现的元素 的位置 c++; //只要是第一次出现此数,就计数 } else { if (step[a] == 0) { step[a] = i - first[a]; //【第一次】出现 某个元素 据第一个与它值相同的元素的步数(即公差) } else if (step[a] > 0 && (i - last[a]) != step[a]) //如果之后出现的元素 据 上一个与它相同的元素的步数 != 公差 { step[a] = -1; //则置为-1,不输出此元素 c--; //不计入此数 } } last[a] = i; //记录这次该元素的位置,以便下次此值再出现时比较步数与公差 } printf("%d\n", c); for (i = 1; i <= maxn - 1; i++) { if (first[i] >= 0) { if (step[i] >= 0) //对于只出现一次的数,step[i] = 0,也会输出 { printf("%d %d\n", i, step[i]); //i为元素a的值,step[i]为公差 } } } } return 0; }
相关文章推荐
- Codeforces 351D:Jeff and Removing Periods
- Codeforce - 352B - Jeff and Periods
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
- codeforces 351B B. Jeff and Furik(概率)
- CodeForces 352C-- Jeff and Rounding
- codeforces 352C Jeff and Rounding 很有趣的一道题
- 【CodeForces 351A】Jeff and Rounding(思维)
- CodeForces 351 B.Jeff and Furik(概率DP)
- CF#204DIV2:B. Jeff and Periods
- cf B. Jeff and Periods
- Codeforces Round #204 (Div. 2) B - Jeff and Periods
- Codeforces 352C Jeff and Rounding【dp】
- B. Jeff and Periods
- CodeForces 352C. Jeff and Rounding(贪心)
- Jeff and Periods
- Codeforces_352A_Jeff and Digits(计数)
- Codeforces_352B_Jeff and Periods(排序)
- Codeforces Round #204 (Div. 2)->B. Jeff and Periods
- codeforces-351B-Jeff and Furik
- Codeforces Round #204 (Div. 2) B. Jeff and Periods