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 ...
随机推荐
- EF架构~在T4模版中自定义属性的getter和setter
回到目录 T4模版为我们在ORM操作上提供了便捷,它很方便的可以对实体进行全局性的修改,之前我介绍过通过T4来为属性加默认性,而今天我主要告诉大家如何使用T4模版将getter,setter块改为自己 ...
- EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~再续~添加对各只读服务器的心跳检测
回到目录 上一讲中基本实现了对数据库的读写分离,而在选择只读数据库上只是随机选择,并没有去检测数据库服务器是否有效,如服务器挂了,SQL服务停了,端口被封了等等,而本讲主要对以上功能进行一个实现,并对 ...
- MyBatis学习总结(五)——实现关联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- [开发工具]Java开发常用的在线工具
注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...
- C#学习系列-抽象方法与虚拟方法的区别
参考:http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=9851&m=9838&ct=31054 如 ...
- C#获取文本文件的编码,自动区分GB2312和UTF8
C# 获取文本文件的编码,自动区分GB2312和UTF8 以下是获取文件编码的一个类 using System; using System.IO; using System.Text; /// < ...
- salesforce 零基础学习(十七)Trigger用法
看本篇之前可以相应阅读以下Trigger相关文章: 1.https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigge ...
- iOS-UICollectionView
1--------------------------------------------------------------------------------------------------- ...
- VS报错:The build tools for v140 (Platform Toolset = 'v140') cannot be found
VS低版本打开高版本常会出现的错: The build tools for v140 (Platform Toolset = 'v140') cannot be found. To build usi ...
- UML基础系列:用例图
1. 概述 用例图(Use Case Diagram)描述“用户.需求.系统功能单元”之间的关系,是参与者所能观察和使用到的系统功能模型图. 用例图用于需求分析阶段 用例图包含6个基本元素:参与者(A ...