CF352B Jeff and Periods 模拟
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.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
int n;
int a[maxn];
struct node {
int st;// 端点
int dx;// 差值
int cnt;// 计数
int fg;
}td[maxn];
map<int, int>mp; int main() {
//ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
td[a[i]].cnt++;
if (td[a[i]].cnt == 1) {
td[a[i]].st = i; td[a[i]].fg = 1;
}
else {
if (td[a[i]].cnt == 2) {
td[a[i]].dx = i - td[a[i]].st;
td[a[i]].st = i;
}
else {
if (td[a[i]].dx != i - td[a[i]].st) {
td[a[i]].fg = 0;
}
else {
td[a[i]].dx = i - td[a[i]].st;
td[a[i]].st = i;
}
}
}
}
sort(a + 1, a + 1 + n);
int tot = unique(a + 1, a + 1 + n) - a - 1;
bool fg = 0; int ct = 0;
for (int i = 1; i <= tot; i++) {
if (td[a[i]].fg == 1) {
ct++;
}
}
if (ct == 0) {
cout << 0 << endl; return 0;
}
cout << ct << endl; for (int i = 1; i <= tot; i++) {
// cout << a[i] << ' ' << td[a[i]].fg << ' ' << td[a[i]].dx << endl;
if (td[a[i]].cnt == 1) {
cout << a[i] << ' ' << 0 << endl;
}
else if (td[a[i]].cnt > 1 && td[a[i]].fg == 1) {
cout << a[i] << ' ' << td[a[i]].dx << endl;
}
}
return 0;
}
CF352B Jeff and Periods 模拟的更多相关文章
- 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> ...
- B. Jeff and Periods(cf)
B. Jeff and Periods time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 B. Jeff and Periods
http://codeforces.com/contest/352/problem/B #include <cstdio> #include <cstring> #includ ...
- 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 351D Jeff and Removing Periods(莫队+区间等差数列更新)
题目链接:http://codeforces.com/problemset/problem/351/D 题目大意:有n个数,每次可以删除掉数值相同并且所在位置成等差数列的数(只删2个数或者只删1个数应 ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
随机推荐
- Git学习笔记(四)标签和搭建Git服务
一.标签是什么 发布一个版本时,我们通常先在版本库中打一个标签,这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一 ...
- CSS2实用知识点详解
CSS相关知识回顾目录 CSS2选择器 假选择器的使用 属性选择器的使用 边框设置 背景设置 字体设置 文本属性 a标签假选择器使用 列表设置 表格设置 鼠标设置 单位设置 隐藏显示 位置设置 清除浮 ...
- 云服务利用Auto Scaling节省30%成本
公有云提供了很多免费的高级功能,很多中小用户以为自己用不上.实际上稍微研究一下,就能享受很多便利和节省不少成本. 本方案就是利用弹性伸缩(auto-scaling)减少服务器成本,几乎适合所有集群式部 ...
- List里将某字段相同的元素合并
工作中遇到将List中某字段相同的元素合并,笔者觉得有必要记录下来,以备以后参考,好了,直接上代码 import java.util.ArrayList; import java.util.List; ...
- DAY7-面向对象之继承与派生
一.初识继承 什么是继承 继承是一种创建新类的方式,新建的类可以继承一个或多个父类(python支持多继承),父类又可称为基类或超类,新建的类称为派生类或子类. 子类会“”遗传”父类的属性,从而解决代 ...
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.
错误信息: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must ...
- JSON 解析出错问题
从 PHP 返回一段 JSON 数据给前台页面,但使用 eval 和 JSON.parse 解析都出错. 在网上那上在线的 json 解析工具解析都正确. 于是一段段删除测试,最终发现问题出在 \r\ ...
- VS调试程序时,程序出现异常,但VS不报错的解决方案
在调试>异常> 里面把勾全勾上就行了!
- Ros学习——创建程序包
1.程序包 一个程序包要想称为catkin程序包必须符合以下要求: 该程序包必须包含catkin compliant package.xml文件 这个package.xml文件提供有关程序包的元信 ...
- Struts2 看1
Struts2部分 1. JavaEE软件三层结构和MVC的区别? JavaEE软件三层机构是由sun公司提供JavaEE开发规范的:Web层(表现层).业务逻辑层.数据持久层.[其中WEB层会使用前 ...