codeforces B. Jeff and Periods 解题报告
题目链接:http://codeforces.com/problemset/problem/352/B
题目意思:给出一个长度为n的序列 a1, a2, ..., an(序号i,1 <= i <= n)。需要从这个序列中,找出符合这两个条件的数:1、这个数在序列 a1, a2, ..., an 中; 2、该数的所有位置(也就是序号i)构成等差数列。一旦有一个位置不满足(此时和上一个位置所求出的公差就与之前的公差不相等),这个数就不符合条件,不应该输出。找完之后,输出所有满足这两个条件的数的总数,还有这些数以及对应的公差。
值得注意的地方有:当这个数在序列中只出现一次的时候,也属于可输出的,此时它的公差为0,也就是ST 1的情况; 还有,整个序列一个都找不到这样的数,要输出0。
这条是我赛中第一次提交的,因为一下子就看懂题目了。不过赛中是没过到啦,以为很简单...赛后,修改,调试,整整两天多,终于AC了。方法比较笨,还是那句话,自己写的特别有感觉。后来看了别人用vector来做,代码长度缩短了很多。看来还是需要学一下容器啊~~~
方法一:
Time | Memory |
---|
92 ms | 2000 KB |
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- const int maxn = 1e5 + ;
- struct pairs
- {
- int index; // 保存位置的编号i
- int num; // 保存序列的数,即题目中的a[i]
- }p[maxn];
- int cmp(pairs a, pairs b)
- {
- if (a.num != b.num)
- return a.num < b.num;
- return a.index < b.index; // 关键!!!保证公差是正数,否则过不了test 10
- }
- int c[maxn], b[maxn];
- int f[maxn];
- int main()
- {
- int i, j, k, n, sum;
- while (scanf("%d", &n) != EOF)
- {
- memset(c, , sizeof(c));
- for (j = i = ; i <= n; i++)
- {
- scanf("%d", &p[i].num);
- c[p[i].num]++; // 统计a[i]在序列中出现多少次
- if (c[p[i].num] == )
- {
- b[j++] = p[i].num; // 保存a[i]
- }
- p[i].index = i;
- }
- k = j;
- sort(p+, p+n+, cmp); // 序列a的数从小到大排序,如果相同,则按具体的位置从小到大排序
- sort(b+, b+j); // 保证输出的序列是递增的
- /* for (i = 1; i <= n; i++)
- {
- printf("p[%d].index = %d, p[%d].num = %d\n", i, p[i].index, i, p[i].num);
- }
- */
- memset(f, , sizeof(f)); // 保存公差
- int tmp, j, flag, minus;
- sum = ;
- for (i = ; i <= n; i += c[p[i].num])
- {
- // printf("i = %d\n", i);
- // printf("c[%d] = %d\n", p[i].num, c[p[i].num]);
- if (c[p[i].num] == ) // 在序列中只出现一次的数,公差设为0
- {
- f[p[i].num] = ;
- sum++; // 也属于可输出的
- }
- else
- {
- flag = ;
- for (j = i; j < c[p[i].num]+i-; j++) // 检测相同数字的公差是否相等
- {
- // printf("j = %d\n", j);
- if (j == i) // 用第一、二个的位置算出公差即可,下面用这个公差来检查其他位置的公差是否和这个公差相等
- {
- tmp = p[j+].index - p[j].index;
- // printf("tmp = %d\n", tmp);
- }
- minus = p[j+].index - p[j].index;
- // printf("minus = %d\n\n", minus);
- if (tmp != minus)
- {
- f[p[i].num] = -; // 发现有一个位置不等于之前算出的公差
- flag = ;
- // printf("error !!!!\n\n");
- }
- }
- if (j == c[p[i].num]+i- && !flag) // 相同的数的所有位置算出的公差都相等
- {
- sum++;
- // printf("success!!!\n");
- f[p[i].num] = tmp; // 保存公差
- }
- }
- }
- if (sum)
- {
- printf("%d\n", sum); // 符合条件的总数
- for (i = ; i < k; i++)
- {
- if (f[b[i]] != -)
- {
- printf("%d %d\n", b[i], f[b[i]]);
- }
- }
- }
- else
- printf("0\n");
- }
- return ;
- }
方法二:
Time | Memory |
---|
92 ms | 4900 KB |
用vector方法写的,记得每次都要清空。
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + ;
- const int maxm = ;
- vector <int> s[maxn];
- int ans[maxn][maxm];
- int main()
- {
- int i, j, n, tmp, flag;
- while (scanf("%d", &n) != EOF)
- {
- memset(s, , sizeof(s));
- for (i = ; i <= n; i++)
- {
- scanf("%d", &tmp);
- s[tmp].push_back(i);
- }
- int d, len, cnt = ;
- for (i = ; i <= maxn; i++)
- {
- flag = ;
- len = s[i].size();
- if (len == )
- continue;
- else if (len == )
- {
- ans[cnt][] = i;
- ans[cnt][] = ;
- cnt++;
- }
- else
- {
- d = s[i][] - s[i][];
- if (len == )
- {
- ans[cnt][] = i;
- ans[cnt][] = d;
- flag = ;
- cnt++;
- }
- // printf("d = %d\n", d);
- if (!flag)
- {
- for (j = ; j < len; j++)
- {
- if (s[i][j] - s[i][j-] != d)
- break;
- }
- if (j == len)
- {
- ans[cnt][] = i;
- ans[cnt][] = d;
- cnt++;
- }
- }
- }
- }
- printf("%d\n", cnt);
- for (i = ; i < cnt; i++)
- {
- printf("%d %d\n", ans[i][], ans[i][]);
- }
- }
- return ;
- }
codeforces B. Jeff and Periods 解题报告的更多相关文章
- codeforces A. Jeff and Digits 解题报告
题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- Codeforces 352B - Jeff and Periods
352B - Jeff and Periods 思路:水题,考验实现(implementation)能力,来一波vector[允悲]. 代码: #include<bits/stdc++.h> ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
随机推荐
- web.xml中/与/*的区别
1.拦截"/",可以实现现在很流行的REST风格.很多互联网类型的应用很喜欢这种风格的URL.为了实现REST风格,拦截了所有的请求.同时对*.js,*.jpg等静态文件的访问也就 ...
- POJ1089 Intervals
Description There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of ...
- bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]
Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在 ...
- Opencv加载和显示图片
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...
- JSP表单处理
当需要通过从浏览器获取一些信息,在许多情况下,最终给到Web服务器后台程序.浏览器使用两种方法将这些信息传递给Web服务器.这些方法是GET方法和POST方法. GET 方法: GET方法将追加到页面 ...
- XUnit学习
1.建立测试单元项目 2.引用XUnit.dll或者在Nuget里安装XUnit 3.安装Nuget->xUnit.net[Runner: Visual Studio] 4.打开 测试-> ...
- vagrant 错误记录
使用Vagrant配置本地开发环境 从二零一四年开始使用vagrant+VirtualBox搭建linux开发环境,配置简单灵活,后台运行占用内存少,比vmware好用很多,果断弃用vmware转投v ...
- ci中如何得到配置的url
$this->load->helper('url'); 然后,你可以用它查询并返回设置在config.php文件中的site和/或base URL: echo site_url(); ec ...
- 初学Hibernate主键生成策略
具有业务含义的主键叫自然主键:随机生成,不具备业务含义的字段作为主键,叫代理主键. 在表与POJO类关系映射文件XXX.hbm.xml中,可通过配置id元素下generator节点的class属性指定 ...
- ios严格检验身份证号码有效性
+ (BOOL)checkIDCard:(NSString *)sPaperId { //判断位数 && sPaperId.length != ) { return NO; } NSS ...