Codeforces Round #204 (Div. 2)->B. Jeff and Periods
1 second
256 megabytes
standard input
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.
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.
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.
1
2
1
2 0
8
1 2 1 3 1 2 1 5
4
1 2
2 4
3 0
5 0
In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
题意:给出有n个数的序列,如果x没有相同的数,就输出x和0,如果有多个x,输出它们坐标的差p[x],这个差是公差(common difference between numbers in the progression)(换句话说,相同的数(>2个)的坐标是等差数列即符合),不是公差的就算数字相同也不符合,比如样例2,1的下标是0 2 4 6,所以它们的p是2,2的下标是1 5,它们的p是4,然而比如6 1 2 2 2 1 1 这个样例,1的坐标是1,5,6,(5-1)!=(6-5),所以1不是,2的坐标的2,3,4,,(3-2)==(4-3),所以2是,即输出为1 2 1;
题解:设三个数组a[](记录下标),flag[](标记符合条件的数),p[](公差),cnt用来累加符合条件的数,然后就遍历,比较相同数坐标的差值是否是公差,好像没什么解释的了。。。
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[]={}, flag[]={}, p[]={};
int n, x, cnt = ;
cin >> n;
for (int i = ; i <= n; i++) {
cin >> x;
if(flag[x])
continue;
if(a[x]) {
if(p[x] == )
p[x] = i - a[x];
else if(p[x] != i - a[x]) {
flag[x] = ;
cnt--; } } else
cnt++;
a[x] = i;
//cout<<i<<x<<cnt<<endl;
}
cout<<cnt<<endl;
for(int i=;i<=;i++){
if(a[i]&&flag[i]==)
printf("%d %d\n",i,p[i]);
}
return ;
}
Codeforces Round #204 (Div. 2)->B. Jeff and Periods的更多相关文章
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...
- Codeforces Round #204 (Div. 2)->C. Jeff and Rounding
C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik
http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- Codeforces Round #204 (Div. 2)->D. Jeff and Furik
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #204 (Div. 2) A.Jeff and Digits
因为数字只含有5或0,如果要被90整除的话必须含有0,否则输出-1 如果含有0的话,就只需考虑组合的数字之和是9的倍数,只需要看最大的5的个数能否被9整数 #include <iostream& ...
- Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律
给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 考虑小数点后面的数字,如果这些数都非零,则就是 abs(原数小数部分相加-1*n), 多一个0 则 m ...
- Codeforces Round #204 (Div. 2)
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
随机推荐
- PHP通过字符串调用函数
1. call_user_func function a($b,$c){ echo $b; echo $c; } call_user_func('a', "111","2 ...
- Python学习教程(learning Python)--1.3 Python数据输入
多数应用程序都有数据输入语句,用于读入数据,和用户进行交互,在Python语言里,可以通过raw_input函数实现数据的从键盘读入数据操作. 基本语法结构:raw_input(prompt) 通常p ...
- C 解决百度知道的一个高中题
前言 今天看见一道百度知道上提问,是这样的. 仔细算了一下, 花了30min.才整出来了,估计现在回去参加高考,数学及格都悬.有时候想做这样的题有什么用, 学这些东西有什么意义,在这种方面浪费时间有什 ...
- STM32单片机实现中断后不继续向下执行而是返回到main函数
做公司的一个项目,实现一个功能就是 机器在进行一项功能时(这项工作时间挺长),想要取消这项工作,重新选择.想了半天没想出来,结果同事提醒了一句,可以在程序中加一个外部中断,在中断中软件复位程序.用到以 ...
- 文件系统 第八次迭代 VFS相关说明
麻烦访问evernote链接 http://www.evernote.com/shard/s133/sh/53e5b5ac-1192-4910-8bd5-6886218562af/59516c32a5 ...
- C++经典书籍推荐
<C++程序设计语言> <C++语言设计与演化> <C++标准程序库> <EFFECTIVE C++ 中文版> <MORE EFFECTIVE C ...
- Tomcat 服务器服务的注册修改删除
1. 注册Tomcat服务 运行cmd,切换目录到tomcat/bin, 执行以下命令service.bat install 2.删除Tomcat服务
- DrawerLayout带有侧滑功能的布局类(1)
DrawerLayout: DrawerLayout顾名思义就是一个管理布局的.使用方式可以与其它的布局类类似. DrawerLayout带有滑动的功能.只要按照drawerLayout的规定布局方式 ...
- 教你怎么安装MongoDB
以下命令以root用户运行:#sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10#echo 'deb http://do ...
- 在线调试js工具网站
http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highch ...