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\n", 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\n", i, step[i]);          //i为元素a的值,step[i]为公差
48                 }
49             }
50         }
51     }
52     return 0;
53 }

 

 

 

转载于:https://www.cnblogs.com/youdiankun/p/3380911.html

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值