紫书 例题 10-29 UVa 1642(最优连续子序列)
2024-08-31 12:16:41
这类求最优连续子序列的题一般是枚举右端点,然后根据题目要求更新左端点,
一般是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;
}
最新文章
- Why is applicationhost.config still being added to source control even thought it's in gitignore
- DOM模型有三种
- [bzoj3155]Preprefix sum(树状数组)
- Protocol Buffers动态消息解析
- javascript 对象和数组(花括号、方括号)
- 操作系统是怎么工作的——mykernel环境的搭建
- javascript 如何继承父类
- Leetcode题解(十八)
- windows server 远程桌面连接问题。
- 【分享】用Canvas实现画板功能
- 开源的许可证GPL、LGPL、BSD、Apache 2.0
- PHP批量生成手机号
- DockerDesktop简单安装和使用
- 【转】解决IDEA新建项目名称为红色
- error:Your local changes to the follwing files would be overwritten by merge
- 四、python之 if while for
- tomcat中server.xml配置详解(转载)(一)
- C++语言基础(18)-模板
- Java EE 学习(9):IDEA + maven + spring 搭建 web(5)- 博客文章管理
- 前端 MV*模式
热门文章
- python 3.x 学习笔记13 (网络编程socket)
- (转载) Android开发时,那些相见恨晚的工具或网站!
- POJ 1064 Cable master (二分答案,G++不过,C++就过了)
- ARM的六大类指令集---LDR、LDRB、LDRH、LDM、STR、STRB、STRH、STM
- jsp表单验证格式
- SpringBoot学习笔记(7)-----CORS支持解决跨域问题
- ArrayList的使用方法
- NodeJS学习笔记 (6)网络服务-http-res(ok)
- 异步调用task
- rem — 一个低调的css单位