2015暑假多校联合---Problem Killer(暴力)
Now you have n problems, the i-th problem's difficulty is represented by an integer ai (1≤ai≤109).
For some strange reason, you must choose some integer l and r (1≤l≤r≤n), and solve the problems between the l-th and the r-th, and these problems' difficulties must form an AP (Arithmetic Progression) or a GP (Geometric Progression).
So how many problems can you solve at most?
You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression
https://en.wikipedia.org/wiki/Geometric_progression
For each test case, the first line contains a single integer n, the second line contains n integers a1,a2,⋯,an.
T≤104,∑n≤106
题意:输入n个数,求其中最长的区间的长度(区间满足等差数列或者等比数列);
思路:两重循环暴力从每个数开始找,但是这样会超时,可以优化一下,记录从当前数开始的等差数列最远的位置,下次从上次记录的最远的位置开始找,这样复杂度会下降很多;
我发现了一个很有意思的事情,我开始写的程序超时了,后来我删掉了一些东西AC了,但我感觉有bug,于是我想了组数据n=5 5个数4 ,8 ,12 , 18 , 27 后面4个数满足等比数列,最长区间应该是4,而我的程序输出是3, 同样这组数据n=6 6个数8 ,16 ,24 , 36 , 54 , 81 后面5个数满足等比数列,输出应该是5,而我的程序输出是4。 我想了一下每次从上次记录的最远的位置开始找,有可能记录的位置的前一位也满足等比数列,所以在计算等比数列长度时得计算一下前一位是否满足;
开始的代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
using namespace std;
long long a[]; int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
int tmp=;
int t,j;
for(int i=;i<n;i=t)
{
for(j=i+;j<n;j++)
{
if(a[j]!=a[i]+(a[i+]-a[i])*(j-i))
break;
}
t=j-;
tmp=max(tmp,j-i); for(j=i+;j<n;j++)
{
if(a[j-]*a[i+]%a[i]==&&a[j]==a[j-]*a[i+]/a[i])
continue;
else break;
}
tmp=max(tmp,j-i);
//cout<<"t: "<<t<<endl;
//if(j-1>i)
//t=min(t,j-1);
if(t==n-)break;
}
printf("%d\n",tmp);
}
return ;
}
后来修改过的代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
using namespace std;
long long a[]; int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
int tmp=;
int t,j;
for(int i=;i<n;i=t)
{
for(j=i+;j<n;j++)
{
if(a[j]!=a[i]+(a[i+]-a[i])*(j-i))
break;
}
t=j-;
tmp=max(tmp,j-i); for(j=i+;j<n;j++)
{
if(a[j-]*a[i+]%a[i]==&&a[j]==a[j-]*a[i+]/a[i])
continue;
else break;
}
int f=;
if(a[i-]*a[i+]%a[i]==&&a[i]==a[i-]*a[i+]/a[i])
f=;
tmp=max(tmp,j-i+f); if(t==n-)break;
}
printf("%d\n",tmp);
}
return ;
}
2015暑假多校联合---Problem Killer(暴力)的更多相关文章
- 2015暑假多校联合---CRB and His Birthday(01背包)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...
- 2015暑假多校联合---Expression(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...
- 2015暑假多校联合---Zero Escape(变化的01背包)
题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 2015暑假多校联合---Friends(dfs枚举)
原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...
- 2015暑假多校联合---Assignment(优先队列)
原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
随机推荐
- Nginx下WordPress的Rewrite
最近接触WP Super Cache,该插件要求固定链接必须是重写的,故用到Rewrite. 我的是这样配置的: /usr/local/nginx/conf/rewrite/wordpress.con ...
- Nginx反向代理搭建配置
1.反向代理方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将服务器上得到的结果返回给internet 上请求连接的客户端,此时代理服务器对外就表现为一个 ...
- JS的脚本语言
js的脚本语言全程javascript在网页里面使用的脚本语言:分类:1.嵌入网页里面2.在外部脚本标签可以写在网页的任何地方,但一般都写在网页的底部:<script type="te ...
- vb6里面dim和set的区别
dim是作用于变量 声明变量并分配存储空间 set作用于对象 将对象引用赋给变量或属性 例子: dim A as collection set A=new collection 等效于 di ...
- 13.首先,编写一个类ChongZai,该类中有3个重载的方法void print();其次, 再编写一个主类来测试ChongZai类的功能。
package java1; //计算器 public class Jisuanqi { //属性 //型号,品牌等 //重载 //1.方法同名不同参 //2.返回类型和重载无关 //3.多态的一种表 ...
- 数据类型,隐式转换以及json,对象,引用类型,预解析 视频教程
随便看看,需要有一点一点基础. 链接:http://pan.baidu.com/s/1c20pcOC 密码:xq2x
- CSS等高布局的6种方式
× 目录 [1]边框模拟 [2]负margin [3]table[4]absolute[5]flex[6]js 前面的话 等高布局是指子元素在父元素中高度相等的布局方式.等高布局的实现包括伪等高和真等 ...
- sqlserver -- 学习笔记(八)体验charindex、stuff 和 for xml path在实际问题中的应用及几个问题的探讨
写在前面 之前做了个微信端顾客扫码评价员工的功能,除了打分数,还可以打标签. 需要统计分数和统计各个员工每种标签被点击的次数. 后来加了个要求,需要查看客户对某个员工一次服务所打出的标签组合. 在不 ...
- 数据结构:JAVA_二叉数查找树基本实现(上)
数据结构:二叉数查找树基本实现(JAVA语言版) 1.写在前面 二叉查找树是一种能将链表插入的灵活性与有序数组查找的高效性结合在一起的一种数据结构. ..... 2.代码分解 2.1 对节点的结构定义 ...
- ASP.NET和IIS工作原理
图为iis6.0运行asp.net的原理. browser向iis发送HTTP请求,HTTP.SYS将其分发给W3SVC(World Wide Web Publishing Service),后者解析 ...