ACM程序设计选修课——1043: Radical loves integer sequences(YY)
来源:互联网 发布:java中的锁 编辑:程序博客网 时间:2024/06/11 16:23
1043: Radical loves integer sequences
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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
1349 9 3 5
Sample Output
13 033 05 09 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));//记录该数的每(前)一次出现的下标poststep=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++;//暂时可行数+1pos[t]=i;//此时为第一次出现的位置step[t]=0;//此时公差为0}else if(mop[t]==2)//两次{step[t]=i-pos[t];//公差Δdpos[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;}
0 0
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
- ACM程序设计选修课——1051: Glamor Sequence(YY+求和公式)
- ACM程序设计选修课——1036: Hungar的菜鸟赛季(YY)
- ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
- ACM程序设计选修课——1049: Efface Numbers(贪心)
- ACM程序设计选修课——1058: Lucky Sequence(思考)
- ACM程序设计选修课——1081: 堆(BFS)
- ACM程序设计选修课——1018: Common Subsequence
- ACM程序设计选修课——1030: Hungar的时尚球场(水题+耐心)
- ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)
- ACM程序设计选修课——1065: Operations on Grids(暴力字符串)
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
- ACM程序设计选修课——1076汇编语言(重定向+模拟)
- ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)
- ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)
- ACM程序设计选修课——1024: 末位零(求末尾0的方法+可有可无的快速幂)
- ACM程序设计选修课——1031: Hungar的得分问题(二)(杨辉三角+二进制转换)
- 进程和线程的区别
- CentOS下Samba文件服务器的安装与配置
- 关于大型网站技术演进的思考(十八)--网站静态化处理—反向代理(10)
- ffmpeg time_base详解
- 关于onTouchEvent跟onInterceptEvent的事件处理
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
- CAShapeLayer和CAGradientLayer
- AndroidStudio常用快捷键
- jquery小技巧(未完待续)
- IOS学习之—— 常用小功能,打电话 发短信 发邮件
- 关于xcode——tabbed
- windowbuilder入门
- BaseAdapter用法详解
- 【LeetCode-24】Swap Nodes in Pairs