1043: Radical loves integer sequences

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 36  Solved: 4
[Submit][Status][Web
Board
]

Description

One day Radical got hold of an integer sequence a1, a2, ..., an of length n. He 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 Radical, 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

1
3
4
9 9 3 5

Sample Output

1
3 0
3
3 0
5 0
9 1

这题前几次看真心没看懂,今天下午看了一下,发现是判断同一个数字出现的下标是否是等差数列,是则输出公差,否则则不输出。

没什么算法,就是记录时判断麻烦点..

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
bool ok[100009];
int mop[100009];
int step[100009];
int pos[100009];
using namespace std;
int main (void)
{
int t,i,j,n,tstep,cnt;
while (~scanf("%d",&n))
{
memset(ok,0,sizeof(ok));//判断改数是否存在等差数列
memset(mop,0,sizeof(mop));//记录该数的出现次数
memset(step,0,sizeof(step));//记录该数的公差
memset(pos,0,sizeof(pos));//记录该数的每(前)一次出现的下标pos
tstep=0;
cnt=0;
for (i=1; i<=n; i++)
{
scanf("%d",&t);
mop[t]++;
if(mop[t]>=1)//若出现过
{
if(mop[t]==1)//若此时只有一次
{
ok[t]=1;//暂时可行
cnt++;//暂时可行数+1
pos[t]=i;//此时为第一次出现的位置
step[t]=0;//此时公差为0
}
else if(mop[t]==2)//两次
{
step[t]=i-pos[t];//公差Δd
pos[t]=i;//位置更变
ok[t]=1;//暂时可行
}
else//三次以上
{
tstep=i-pos[t];//临时公差
if(tstep!=step[t])//若不等于此前的公差,则
{
if(ok[t])//若此前没有被减掉
cnt--;//减掉该数
ok[t]=0;//变为不可行
}
pos[t]=i;//位置变更
}
}
}
printf("%d\n",cnt);
for (i=1; i<=100000; i++)
{
if(ok[i])
{
if(mop[i]==1)
{
printf("%d %d\n",i,0);
}
else
{
printf("%d %d\n",i,step[i]);
}
}
}
}
return 0;
}

ACM程序设计选修课——1043: Radical loves integer sequences(YY)的更多相关文章

  1. ACM程序设计选修课——1018: Common Subsequence(DP)

    问题 L: Common Subsequence 时间限制: 1 Sec  内存限制: 32 MB 提交: 70  解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ...

  2. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  3. ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)

    1040: Alex and Asd fight for two pieces of cake Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 27   ...

  4. ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)

    问题 E: (ds:图)公路村村通 时间限制: 1 Sec  内存限制: 128 MB 提交: 9  解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...

  5. ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)

    问题 F: (ds:图)旅游规划 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 4 题目描述 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路 ...

  6. ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)

    Problem D: (ds:树)合并果子 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 80  Solved: 4 [Submit][Status][ ...

  8. ACM程序设计选修课——1076汇编语言(重定向+模拟)

    1076: 汇编语言 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 34  Solved: 4 [Submit][Status][Web Board] ...

  9. ACM程序设计选修课——1065: Operations on Grids(暴力字符串)

    1065: Operations on Grids Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 17  Solved: 4 [Submit][Sta ...

随机推荐

  1. Java变量、Java对象初始化顺序

    局部变量与成员变量: 局部变量分为: 行参:在方法签名中定义的局部变量,随方法的结束而凋亡. 方法内的局部变量:必须在方法内对其显示初始化,从初始化后开始生效,随方法的结束而凋亡. 代码块内的局部变量 ...

  2. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep

    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...

  3. mini_batch GD

    工作过程:训练总样本个数是固定的,batch_size大小也是固定的,但组成一个mini_batch的样本可以从总样本中随机选择.将mini_batch中每个样本都经过前向传播和反向传播,求出每个样本 ...

  4. 读书笔记-《深入理解Java虚拟机:JVM高级特性与最佳实践》

    目录 概述 第一章: 走进Java 第二章: Java内存区域与内存溢出异常 第三章: 垃圾收集器与内存分配策略 第四章: 虚拟机性能监控与故障处理 第五章: 调优案例分析与实战 第六章: 类文件结构 ...

  5. java基础—接口概念

    一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如“金丝猴是一种动物”,金丝猴从动物这个类继承,同时“金丝猴是一种值钱的东西”,金丝猴从“值钱的东西”这个类继承,同时“金丝猴 ...

  6. iOS开发之WIFI,3G/4G两种网络同时使用技巧

    最近遇到一个比较奇葩的需求:App与硬件通过WiFi LAN通信, 同时App需要与服务器通过3G/4G WAN通信,如下图: 众所周知,手机同时打开WiFi和3G时候,会优先走WiFi.这个该如何实 ...

  7. 201621123080《java程序设计》第14周实验总结

    201621123080<java程序设计>第14周实验总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2. ...

  8. drf 认证功能

    drf(django rest-framework)认证组件 复习 HyperlinkedIdentityField ​```python 功能:快速生成连接 1. publish = seriali ...

  9. HTTP认证之基本认证——Basic(二)

    导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 在HTTP认证 ...

  10. Phonegap环境配置和安装插件

    一:安装好jdk(配置好环境变量) 二:安装好Android SDK(配置好环境变量path F:\Android\android-sdk-windows\platform-tools;F:\Andr ...