区间 (模拟并查集优化)

问题描述

给出一个序列 a1, ..., an。
定义一个区间 [l,r] 是好的,当且仅当这个区间中存在一个 i,使得 ai 恰好等于 al, al+1, ...,
ar-1, ar 的最大公因数。
求最长的好的区间的长度。

输入

第一行 n,表示序列的长度;
第二行 n 个数 a1,a2,...,an。

输出

输出一行一个数,表示最长的好的区间的长度。

题解

    题解写代码里可能容易懂?

代码

#include <cstdio>
#define ll long long
#define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while (c>'9'||c<'0') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') {x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return x*f;
}
const int maxn(4e6+5);
ll n,a[maxn],l[maxn],r[maxn],ans;

void init(){
    n=read();
    for (int i(1);i<=n;++i) a[i]=read();
}

inline ll max(ll a,ll b){return a>b?a:b;}

int main(){
//  file("block");
    init();
    for (int i(1);i<=n;++i) for (l[i]=i;1<l[i]&&a[l[i]-1]%a[i]==0;l[i]=l[l[i]-1]);//向左扩展,满足左一项是右一项的倍数,则扩展,模拟并查集优化
    for (int i(n);i>=1;--i) for (r[i]=i;r[i]<n&&a[r[i]+1]%a[i]==0;r[i]=r[r[i]+1]);//向右扩展,同上
    for (int i(1);i<=n;++i) ans=max(ans,r[i]-l[i]+1);//计算答案:最长区间
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/cancers/p/11565053.html

时间: 2024-12-29 05:42:38

区间 (模拟并查集优化)的相关文章

普林斯顿公开课 算法1-10:并查集-优化的快速合并方法

应用 渗透问题 游戏中会用到. 动态连接 最近共同祖先 等价有限状态机 物理学Hoshen-Kopelman算法:就是对网格中的像素进行分块 Hinley-Milner多态类型推断 Kruskai最小生成树 Fortran等价语句编译 形态学开闭属性 Matlab中关于图像处理的bwlabel函数 渗透问题 一个N×N的矩阵,判断顶部和底部是否连通就是渗透问题. 下图中左侧的矩阵能渗透,右侧矩阵不能渗透. 渗透问题在电学.流体力学.社会交际中都有应用. 在游戏中可能需要生成一张地图,但是作为地图

(贪心 + 并查集优化) poj 1456

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9452   Accepted: 4067 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an int

ZOJ 2833-Friendship(并查集+优化)

Friendship Time Limit: 3 Seconds      Memory Limit: 32768 KB A friend is like a flower, a rose to be exact, Or maybe like a brand new gate that never comes unlatched. A friend is like an owl, both beautiful and wise. Or perhaps a friend is like a gho

Supermarket poj 1456 贪心+并查集优化

Language: Default Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 4096 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is

POJ 1456——Supermarket——————【贪心+并查集优化】

Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx

poj 1827 A Bunch Of Monsters 贪心(并查集优化)

Description Background Jim is a brave explorer. One day, he set out for his next destination, a mysterious hill. When he arrived at the foot of the hill, he was told that there were a bunch of monsters living in that hill, and was dissuaded from cont

Supermarket---poj456(贪心并查集优化)

题目链接:http://poj.org/problem?id=1456 题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了: 暴力的方法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #i

并查集优化连边

很多题目均可用并查集优化连边, 跳过一些已经访问过的点 比方说, 对[L,R]范围进行一定更新可以这样写 for (int i=Find(L); i<=R; i=Find(i)) { //do something fa[i] = i+1; } 这样操作过后[L,R]的fa均指向R+1, 下一次会直接跳到R+1, 相当于每个点值只更新一次 例题 1, CF 827A 求构造一个最小字典序的字符串, 其中字符串某些位置的字符固定 #include <iostream> #define REP

POJ 1456 Supermarket 区间问题并查集||贪心

F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Appoint description:  System Crawler  (2015-11-30) Description A supermarket has a set Prod of products on sale. It earns a p