题目链接

Problem Description

Davy Jones has captured another ship and is smiling contently under the sun. Today is a good day for him. He will get more souls to serve on his crew. The day, however, looks so nice, the sun shining brightly above all and the ocean spilled calmly around, that he decides to be merciful and give some

chance to the wretched seamen of the captured ship. He decides to play the following game.

He will line a group of captives in front of him, and then write a number of his choice on each

man’s forehead. After that, he wants to check if the set of numbers is 3-free. What does it mean for a set to be 3-free? It means that there are no 3 numbers in the set that form an increasing arithmetic progression (weird games have damned Jones, aye), i.e., a sequence of numbers such that the difference of any two successive members of the sequence is a positive constant, such as {1 2 3}, {4 6 8}, or {-2, 1, 4}. If the the set of numbers on men’s foreheads is 3-free, the group is free, too (what an irony).

However, if not, those who have the numbers of the lexicographically first triple that proves (i.e. is a witness) that the set is not 3-free will serve on Jones’ crew for an eternity.

And you ... You will be Jones’ assistant in this game. Checking each group whether it is 3-free or not. And you’d better check right or you will end up on Jones’ crew as well.

A triple (a1, a2, a3) is an increasing arithmetic progression if a2-a1=a3-a2, and a2-a1>0.

A triple (a1, a2, a3) is lexicographically smaller than (b1, b2, b3) if

a1 < b1, or

a1 = b1 and a2 < b2, or

a1 = b1, a2 = b2 and a3 < b3.

Note that you will be looking at triples (a1, a2, a3) of increasing order, i.e. a1 ≤ a2 ≤ a3. The numbers on men’s foreheads need not be in increasing order though.

Input

Input consists of a single line with the numbers written on the captured men’s foreheads. The first number of the line denotes how many men are there in the group(it will not exceed 10000) and should not be included in the sequence.

Output

Output should consist of a single line. If the sequence of numbers in the input is 3-free, output “Sequence is 3-free.” If the sequence is not 3-free, output “Sequence is not 3-free. Witness: w1, w2, w2.” (note the intervals after the first dot and the colon), where w1, w2, w3 is the lexicographically first witness that the sequence is not 3-free.

Sample Input`

4 1 5 6 8

7 1 3 5 2 -7 0 -1`

Sample Output`

Sequence is 3-free.

Sequence is not 3-free. Witness: -7,-1,5.`

题目分析:

要求给出的一组数据中任意的三个数不能构成"3-free"(即三个数不能够是公差大于零的等差数列),为了方便查找我们可以先把给出的这组数据按照升序排列,然后循环找出首项和末项,再判断他们的中间项是不是在这组数据中。

找中间项时如果还用for循环查找的话,相当于三层for循环嵌套,所需要的运行时间太大,可以用set容器或则map容器优化这个查找过程。

三层for循环嵌套(超时)

    #include<iostream>
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
int a[10009];
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(&a[0],&a[n]);//因为要看能否构成等差数列我们
//先给数据排序这样方便后面的比较
int op=0; //op作为一个标记,看看后面是否满足条件的
for(i=0; i<n; i++)
{
for(j=i+2; j<n; j++)
{
int m,bj=0;
m=(a[i]+a[j])/2;
for(int k=0; k<n-1; k++) // 因为多加了一个找中间值的循环,运行时间会超限
{
if(a[k]==m)
{
bj=1;
break;
}
}
if(bj==1)
{
if((a[i]+a[j])%2==0)//保证中间的那个数是满足条件的整数
{
op=1;
printf("Sequence is not 3-free. Witness: %d,%d,%d.\n",a[i],(a[i]+a[j])/2,a[j]);
break;
}
}
}
if(op==1)break;
}
if(op==0)
printf("Sequence is 3-free.\n");
}
return 0;
}

set容器优化

    #include<iostream>
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
int a[10009];
int main()
{
int n,i,j;
set<int>s;
while(~scanf("%d",&n))
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
s.insert(a[i]);
}
sort(&a[0],&a[n]);
int op=0;
for(i=0; i<n; i++)
{
for(j=i+2; j<n; j++)
{
if((a[i]+a[j])%2==0&&s.find((a[i]+a[j])/2)!=s.end())//保证中间的那个数是满足条件的整数,
//且存在于输入的数据中
{
op=1;
printf("Sequence is not 3-free. Witness: %d,%d,%d.\n",a[i],(a[i]+a[j])/2,a[j]);
break;
}
}
if(op==1)break;
}
if(op==0)
printf("Sequence is 3-free.\n");
}
return 0;
}

map容器优化

    #include<iostream>
#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
int a[10009];
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
map<int,int>m;
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
m[a[i]]=1;
}
sort(&a[0],&a[n]);
int op=0; //op作为一个标记,看看后面是否满足条件的
for(i=0; i<n; i++)
{
for(j=i+2; j<n; j++)
{
if((a[j]+a[i])%2==0&&m[(a[j]+a[i])/2]==1)//保证中间的那个数存在
{
op=1;
printf("Sequence is not 3-free. Witness: %d,%d,%d.\n",a[i],(a[i]+a[j])/2,a[j]);
break;
}
} if(op==1)break;
}
if(op==0)
printf("Sequence is 3-free.\n");
}
return 0;
}

HDU 2593 Pirates’ Code (STL容器)的更多相关文章

  1. Windows中VS code无法查看C++ STL容器的值 - 解决方法

    Windows中VS code debug时无法查看C++ STL容器内容 首先,你很可能用的是x64版本的Windows. 我发现一个有效的解决方法,但在x64版本的Windows上安装MinGW时 ...

  2. STL容器

    啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...

  3. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  4. STL容器删除元素的陷阱

    今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector< ...

  5. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  6. GDB打印STL容器内容

    GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...

  7. STL容器迭代器失效分析

    连续内存序列容器(vector, string, deque) 对于连续内存序列STL容器,例如vector,string,deque,删除当前iterator会使得后面所有的iterator都失效, ...

  8. STL容器的适用情况

     转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...

  9. STL容器的遍历删除

    STL容器的遍历删除 今天在对截包程序的HashTable中加入计时机制时,碰到这个问题.对hash_map中的每个项加入时间后,用查询函数遍历hash_map,以删除掉那些在表存留时间比某个阈值长的 ...

随机推荐

  1. QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写   ...

  2. 2nd 本周例行报告

    每周例行报告 1.个人项目:词频统计更新 C类型 C内容 S开始时间 E结束时间 I间隔(min) T净时间(min) 分析 功能分析 8:30 10:00 20 70 学习 查阅资料 10:00 1 ...

  3. 第129天:node.js安装方法

    node.js安装方法 第一步:双击node.js安装包开始安装,注意64位和32位,按照自己的进行安装 第二步:在安装过程中一直选择next,在选择安装目录时,大多数默认安装在C盘,我安装在了D盘, ...

  4. iOS 通过网络请求获取图片的下载歌曲

    1.导入代理<NSURLConnectionDataDelegate> @interface ViewController ()<NSURLConnectionDataDelegat ...

  5. bzoj2820-GCD

    题意 \(T\le 10^4\) 次询问 \(n,m\) ,求 \[ \sum _{i=1}^n\sum _{j=1}^m[gcd(i,j)\text { is prime}] \] 分析 这题还是很 ...

  6. bzoj1061-[Noi2008]志愿者招募-单纯形 & 费用流

    有\(n\)天,\(m\)类志愿者,一个第\(i\)类志愿者可以从第\(s_i\)天工作到第\(t_i\)天,第\(i\)天工作的志愿者不少于\(b_i\)个.每一类志愿者都有单价\(c_i\),问满 ...

  7. 【BZOJ2436】【NOI2011】NOI嘉年华(动态规划)

    [BZOJ2436]NOI嘉年华(动态规划) 题面 BZOJ 题解 考虑第一问如何求解 发现状态与选择了哪些活动无关,只与时间有关 设\(f[i][j]\)表示前\(i\)个单位时间(离散后),一个嘉 ...

  8. ZJOI2018 D1

    归途的车上满是悲伤的气息 虽然早就预言到D1会滚粗,但一切都结束之后还是特别难过. 延时15min 50min T1 30pts 1.5h T2 10pts 2.5h T1 50pts 4.5h T3 ...

  9. 洛谷P1273 有线电视网 【树上分组背包】

    题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...

  10. Dirichlet 卷积学习笔记

    Dirichlet 卷积学习笔记 数论函数:数论函数亦称算术函数,一类重要的函数,指定义在正整数集上的实值或复值函数,更一般地,也可把数论函数看做是某一整数集上定义的函数. 然而百科在说什么鬼知道呢, ...