题目链接: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
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. using namespace std;
  7.  
  8. const int maxn = 1e5 + ;
  9.  
  10. struct pairs
  11. {
  12. int index; // 保存位置的编号i
  13. int num; // 保存序列的数,即题目中的a[i]
  14. }p[maxn];
  15.  
  16. int cmp(pairs a, pairs b)
  17. {
  18. if (a.num != b.num)
  19. return a.num < b.num;
  20. return a.index < b.index; // 关键!!!保证公差是正数,否则过不了test 10
  21. }
  22.  
  23. int c[maxn], b[maxn];
  24. int f[maxn];
  25.  
  26. int main()
  27. {
  28. int i, j, k, n, sum;
  29. while (scanf("%d", &n) != EOF)
  30. {
  31. memset(c, , sizeof(c));
  32. for (j = i = ; i <= n; i++)
  33. {
  34. scanf("%d", &p[i].num);
  35. c[p[i].num]++; // 统计a[i]在序列中出现多少次
  36. if (c[p[i].num] == )
  37. {
  38. b[j++] = p[i].num; // 保存a[i]
  39. }
  40. p[i].index = i;
  41. }
  42. k = j;
  43. sort(p+, p+n+, cmp); // 序列a的数从小到大排序,如果相同,则按具体的位置从小到大排序
  44. sort(b+, b+j); // 保证输出的序列是递增的
  45. /* for (i = 1; i <= n; i++)
  46. {
  47. printf("p[%d].index = %d, p[%d].num = %d\n", i, p[i].index, i, p[i].num);
  48. }
  49. */
  50. memset(f, , sizeof(f)); // 保存公差
  51. int tmp, j, flag, minus;
  52. sum = ;
  53. for (i = ; i <= n; i += c[p[i].num])
  54. {
  55. // printf("i = %d\n", i);
  56. // printf("c[%d] = %d\n", p[i].num, c[p[i].num]);
  57. if (c[p[i].num] == ) // 在序列中只出现一次的数,公差设为0
  58. {
  59. f[p[i].num] = ;
  60. sum++; // 也属于可输出的
  61. }
  62. else
  63. {
  64. flag = ;
  65. for (j = i; j < c[p[i].num]+i-; j++) // 检测相同数字的公差是否相等
  66. {
  67. // printf("j = %d\n", j);
  68. if (j == i) // 用第一、二个的位置算出公差即可,下面用这个公差来检查其他位置的公差是否和这个公差相等
  69. {
  70. tmp = p[j+].index - p[j].index;
  71. // printf("tmp = %d\n", tmp);
  72. }
  73. minus = p[j+].index - p[j].index;
  74. // printf("minus = %d\n\n", minus);
  75. if (tmp != minus)
  76. {
  77. f[p[i].num] = -; // 发现有一个位置不等于之前算出的公差
  78. flag = ;
  79. // printf("error !!!!\n\n");
  80.  
  81. }
  82. }
  83. if (j == c[p[i].num]+i- && !flag) // 相同的数的所有位置算出的公差都相等
  84. {
  85. sum++;
  86. // printf("success!!!\n");
  87. f[p[i].num] = tmp; // 保存公差
  88. }
  89. }
  90. }
  91. if (sum)
  92. {
  93. printf("%d\n", sum); // 符合条件的总数
  94. for (i = ; i < k; i++)
  95. {
  96. if (f[b[i]] != -)
  97. {
  98. printf("%d %d\n", b[i], f[b[i]]);
  99. }
  100. }
  101. }
  102. else
  103. printf("0\n");
  104. }
  105. return ;
  106. }

方法二:

Time Memory
92 ms 4900 KB

用vector方法写的,记得每次都要清空。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. const int maxn = 1e5 + ;
  9. const int maxm = ;
  10.  
  11. vector <int> s[maxn];
  12. int ans[maxn][maxm];
  13.  
  14. int main()
  15. {
  16. int i, j, n, tmp, flag;
  17. while (scanf("%d", &n) != EOF)
  18. {
  19. memset(s, , sizeof(s));
  20. for (i = ; i <= n; i++)
  21. {
  22. scanf("%d", &tmp);
  23. s[tmp].push_back(i);
  24. }
  25. int d, len, cnt = ;
  26. for (i = ; i <= maxn; i++)
  27. {
  28. flag = ;
  29. len = s[i].size();
  30. if (len == )
  31. continue;
  32. else if (len == )
  33. {
  34. ans[cnt][] = i;
  35. ans[cnt][] = ;
  36. cnt++;
  37. }
  38. else
  39. {
  40. d = s[i][] - s[i][];
  41. if (len == )
  42. {
  43. ans[cnt][] = i;
  44. ans[cnt][] = d;
  45. flag = ;
  46. cnt++;
  47. }
  48. // printf("d = %d\n", d);
  49. if (!flag)
  50. {
  51. for (j = ; j < len; j++)
  52. {
  53. if (s[i][j] - s[i][j-] != d)
  54. break;
  55. }
  56. if (j == len)
  57. {
  58. ans[cnt][] = i;
  59. ans[cnt][] = d;
  60. cnt++;
  61. }
  62. }
  63. }
  64. }
  65. printf("%d\n", cnt);
  66. for (i = ; i < cnt; i++)
  67. {
  68. printf("%d %d\n", ans[i][], ans[i][]);
  69. }
  70. }
  71. return ;
  72. }

codeforces B. Jeff and Periods 解题报告的更多相关文章

  1. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  2. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  3. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  4. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  5. Codeforces 352B - Jeff and Periods

    352B - Jeff and Periods 思路:水题,考验实现(implementation)能力,来一波vector[允悲]. 代码: #include<bits/stdc++.h> ...

  6. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  7. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  8. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  9. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

随机推荐

  1. web.xml中/与/*的区别

    1.拦截"/",可以实现现在很流行的REST风格.很多互联网类型的应用很喜欢这种风格的URL.为了实现REST风格,拦截了所有的请求.同时对*.js,*.jpg等静态文件的访问也就 ...

  2. POJ1089 Intervals

    Description There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of ...

  3. bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]

    Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在 ...

  4. Opencv加载和显示图片

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...

  5. JSP表单处理

    当需要通过从浏览器获取一些信息,在许多情况下,最终给到Web服务器后台程序.浏览器使用两种方法将这些信息传递给Web服务器.这些方法是GET方法和POST方法. GET 方法: GET方法将追加到页面 ...

  6. XUnit学习

    1.建立测试单元项目 2.引用XUnit.dll或者在Nuget里安装XUnit 3.安装Nuget->xUnit.net[Runner: Visual Studio] 4.打开 测试-> ...

  7. vagrant 错误记录

    使用Vagrant配置本地开发环境 从二零一四年开始使用vagrant+VirtualBox搭建linux开发环境,配置简单灵活,后台运行占用内存少,比vmware好用很多,果断弃用vmware转投v ...

  8. ci中如何得到配置的url

    $this->load->helper('url'); 然后,你可以用它查询并返回设置在config.php文件中的site和/或base URL: echo site_url(); ec ...

  9. 初学Hibernate主键生成策略

    具有业务含义的主键叫自然主键:随机生成,不具备业务含义的字段作为主键,叫代理主键. 在表与POJO类关系映射文件XXX.hbm.xml中,可通过配置id元素下generator节点的class属性指定 ...

  10. ios严格检验身份证号码有效性

    + (BOOL)checkIDCard:(NSString *)sPaperId { //判断位数 && sPaperId.length != ) { return NO; } NSS ...