这类求最优连续子序列的题一般是枚举右端点,然后根据题目要求更新左端点,

一般是nlogn,右端点枚举是n,左端点是logn

难点在于如何更新左端点

用一些例子试一下可以发现

每次加进一个新元素的时候

前面的元素都要再取一遍gcd

然后gcd同的时候i尽量大

为了去重,我们需要排序,用相邻元素比较

保留下来最优的一些区间,然后更新答案

#include<cstdio>
#include<algorithm>
#include<vector>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std; typedef long long ll;
const int MAXN = 112345;
ll a[MAXN]; struct node
{
ll g;
int p;
bool operator < (const node& rhs) const
{
return (g < rhs.g) || (g == rhs.g && p < rhs.p);
}
}; ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } int main()
{
int n, T;
scanf("%d", &T); while(T--)
{
scanf("%d", &n);
REP(i, 0, n) scanf("%lld", &a[i]);
vector<node> g;
ll ans = 0; REP(i, 0, n)
{
g.push_back(node{0, i});
REP(j, 0, g.size())
g[j].g = gcd(g[j].g, a[i]);
sort(g.begin(), g.end()); vector<node> newg;
REP(j, 0, g.size())
if(j == 0 || g[j].g != g[j-1].g)
{
newg.push_back(g[j]);
ans = max(ans, g[j].g * (i - g[j].p + 1));
}
g = newg;
} printf("%lld\n", ans);
} return 0;
}

最新文章

  1. Why is applicationhost.config still being added to source control even thought it's in gitignore
  2. DOM模型有三种
  3. [bzoj3155]Preprefix sum(树状数组)
  4. Protocol Buffers动态消息解析
  5. javascript 对象和数组(花括号、方括号)
  6. 操作系统是怎么工作的——mykernel环境的搭建
  7. javascript 如何继承父类
  8. Leetcode题解(十八)
  9. windows server 远程桌面连接问题。
  10. 【分享】用Canvas实现画板功能
  11. 开源的许可证GPL、LGPL、BSD、Apache 2.0
  12. PHP批量生成手机号
  13. DockerDesktop简单安装和使用
  14. 【转】解决IDEA新建项目名称为红色
  15. error:Your local changes to the follwing files would be overwritten by merge
  16. 四、python之 if while for
  17. tomcat中server.xml配置详解(转载)(一)
  18. C++语言基础(18)-模板
  19. Java EE 学习(9):IDEA + maven + spring 搭建 web(5)- 博客文章管理
  20. 前端 MV*模式

热门文章

  1. python 3.x 学习笔记13 (网络编程socket)
  2. (转载) Android开发时,那些相见恨晚的工具或网站!
  3. POJ 1064 Cable master (二分答案,G++不过,C++就过了)
  4. ARM的六大类指令集---LDR、LDRB、LDRH、LDM、STR、STRB、STRH、STM
  5. jsp表单验证格式
  6. SpringBoot学习笔记(7)-----CORS支持解决跨域问题
  7. ArrayList的使用方法
  8. NodeJS学习笔记 (6)网络服务-http-res(ok)
  9. 异步调用task
  10. rem — 一个低调的css单位