B. Jeff and Periods(cf)
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
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
const int Max=;
using namespace std;
int main()
{
int n,x;
int vis[Max],p[Max];
while(~scanf("%d",&n))
{
int k = ,i,j;
vector<int>G[Max];
memset(vis,,sizeof(vis));
for (i = ; i < n; i++)
{
scanf("%d",&x);
G[x].push_back(i);//将所有x的位置存入vector中
if (!vis[x])
{
vis[x] = ;
p[k++] = x;
} }
int cnt = ;
memset(vis,-,sizeof(vis));
for (j = ; j < k; j++)
{
int len = G[p[j]].size();
if (len==)
{
++cnt;
vis[p[j]]= ;
continue;
}
int d = G[p[j]][]-G[p[j]][];//求公差
for (i = ; i < len; i++)
{
if (G[p[j]][i]-G[p[j]][i-]!=d)
break;
}
if (i >= len)//说明p[j]的各位置是等差数列
{
++cnt;
vis[p[j]] = d;//表示p[j]各位置的公差为d
}
}
printf("%d\n",cnt);
sort(p, p+k);
for (i = ; i < k ; i++)
{
if (vis[p[i]]!=-)
{
printf("%d %d\n",p[i],vis[p[i]]);
}
}
}
return ;
}
B. Jeff and Periods(cf)的更多相关文章
- Codeforces Round #204 (Div. 2)->B. Jeff and Periods
B. Jeff and Periods time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 352B - Jeff and Periods
352B - Jeff and Periods 思路:水题,考验实现(implementation)能力,来一波vector[允悲]. 代码: #include<bits/stdc++.h> ...
- cf B. Jeff and Periods
http://codeforces.com/contest/352/problem/B #include <cstdio> #include <cstring> #includ ...
- CF352B Jeff and Periods 模拟
One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decide ...
- A. Jeff and Digits(cf)
A. Jeff and Digits time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- codeforces B. Jeff and Periods 解题报告
题目链接:http://codeforces.com/problemset/problem/352/B 题目意思:给出一个长度为n的序列 a1, a2, ..., an(序号i,1 <= i ...
- code forces Jeff and Periods
/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...
随机推荐
- 可以用作javascript异步模式的函数写法
1. 回调函数 f1(); f2(); function f1(callback) { setTimeout(function() { // f1的任务代码 callback(); }, 1000); ...
- Django - 一对多数据示例
1.增加Host -id 可以在模版中增加代码: 备注: 1.counter (从1开始) 2.counter0(从0开始) 3.revcounter(倒序) 4.revcounter0(倒序从0开始 ...
- Exception总结
NO.1 java.lang.NullPointerException 程序遇上了空指针 NO.2 java.lang.ClassNotFoundException 指定的类不存在 NO.3 java ...
- Linux常用解压缩命令
压 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称 查 询:tar -jtv -f filename.tar.bz2 解压缩:tar -jxv -f filen ...
- 15.most_fields策略进行cross-fields search
主要知识点: cross-fields 的使用场景 cross-fields 使用方法 cross-fields 的缺点 一.cross-fields 的使用场景 cross-fiel ...
- 玲珑杯1147 - 最后你还是AK了
1147 - 最后你还是AK了 Time Limit:5s Memory Limit:256MByte DESCRIPTION 今天HHHH遇到了一颗树,这个树有nn个点(nn为偶数),每条边都有一个 ...
- XOR Queries
XOR Queries 时间限制: 1000ms 内存限制: 256M 描述 给出一个长度为n的数组C,回答m个形式为(L,R,A,B)的询问,含义为存在多少个不同的数组下标k∈[L,R]满足C[ ...
- mybatis源码阅读-初始化六个工具(六)
六个基本工具图集 图片来源:https://my.oschina.net/zudajun/blog/668596 ObjectFactory 类图 接口定义 public interface Obje ...
- [HZOI 2016]tree—增强版
[HZOI 2016]tree—增强版 注意事项 为了体现增强版,题目限制和数据范围有所增强: 时间限制:1.5s 内存限制:128MB 对于15% 的数据,1<=N,Q<=1000. 对 ...
- N天学习一个linux命令之df
用途 查看系统硬盘空间使用情况 用法 df [OPTION]... [FILE]... 常用参数 -a, --all 显示所有文件系统,包含类似文件系统(dummy file system) -B, ...