题目链接: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 解题报告的更多相关文章

  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. 【codevs1409】 拦截导弹 2

    http://codevs.cn/problem/1409/ (题目链接) 题意 给出n个三维的导弹,每次拦截只能打x,y,z严格上升的若干个导弹,求最多能一次拦截下多少个导弹,以及最少拦截几次将所有 ...

  2. slf4j和log4j配置

    SLF4J即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.按照官方的说法,SLF4J是一个用于日志系统的简单Fac ...

  3. 轻量级应用开发之(01)第一个IOS程序

    一 IPhone轻量级开发 1. 开发环境 Mac 版本: OS X EICap 10.11.3 (15D21) XCode开发版本: Version 7.2.1 (7C1002) 2.简单分析 UI ...

  4. std::thread join和detach区别

    thread detach, join 线程有两种状态,joinable或者detachable,pthread默认创建的线程是joinable的,也可以指定atrribute创建成一个detacha ...

  5. SSH整合之spring整合hibernate

    SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...

  6. 织梦DedeCms去掉栏目页面包屑导航最后的分隔符“>”

    织梦DedeCms的面包屑导航调用标签{dede:field name=’position’ /},在栏目页里调用的面包屑导航,最后会出现分割符号“>”,如:主页 > DedeCms 模板 ...

  7. Tomcat入门指南

    转自javaresearch.com由timgball 整理 Tomcat是一个免费的开源Web服务器,最新版本是5.5.1,支持Servlet2.4,JSP2.0,非常适合初学者学习Java Web ...

  8. SOA面向服务架构简述

    在上篇中我们简单谈了下架构设计中服务层的简单理解,在这里我们将继续服务层的架构,在本节我们将重点在于分布式服务.在分布式系统中表现层和业务逻辑层 并不处于同一物理部署,所以我们必须存在分布式服务,以契 ...

  9. 完美串(区间dp)

    完美串 Description 爱美之心人皆有之,GG也不例外.所以GG他对于完美串有一种热衷的爱.在GG眼中完美串是一个具有无比魅力的01子串.这个子串有之其魅力之处,对它取反后水平翻转,它又和它原 ...

  10. PHP数字格式化,每三位逗号分隔数字,可以保留小数

    在报价的时候为了给浏览者更清晰明确的数字,所以需要用到数字格式化,有两种方法,一种自己写函数,另一种当然是系统自带的,其实我更喜欢系统自带的. 先来系统简单的: string number_forma ...