CodeChef Subarray LCM

1 篇文章 0 订阅

传送门

portal to editorial

这题 一开始因为写了经过优化的素数筛导致WA了一个多小时...

两种较好写的写法(其实是同一种做法)

1.dp

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <stack>
#include <bitset>
#include <vector>

using namespace std;
const int maxn = 1e6 + 500;
bool vis[maxn];
int prime[maxn/2], cnt, dp[maxn], a[maxn], pos[maxn];
vector<int> VI[maxn];
void sieve() {
    cnt = 0;
    for(int i = 2; i < maxn; i++) {
        if(!vis[i]) {
            prime[cnt++] = i;
            VI[i].push_back(i);
            for(int j = 2; j * i < maxn; j++) {
                vis[j * i] = 1;
                VI[j * i].push_back(i);
            }
        }
    }
}
int calc(int x) {
    int t = 0;
    //printf("%d", a[x]);
    for(int i = 0; i < VI[a[x]].size(); i++) {
        t = max(t, pos[VI[a[x]][i]]);
        //printf("%d ", VI[a[x]][i]);
        pos[VI[a[x]][i]] = x;
    }
    return t;
}
int main() {
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    sieve();
    int T;
    scanf("%d", &T);
    while(T--) {
        int n, ans = -1;
        scanf("%d", &n);
        memset(dp, 0, sizeof dp);
        memset(pos, 0, sizeof pos);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        for(int i = 1; i <= n; i++) {
            int t = calc(i);
            dp[i] = min(dp[i-1] + 1, i - t);
            //printf("%d %d %d\n", a[i], dp[i], t);
            ans = max(ans, dp[i]);
        }
        printf("%d\n", ans < 2 ? -1 : ans);
    }
    return 0;
}

2.双指针

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <stack>
#include <bitset>
#include <vector>

using namespace std;
const int maxn = 1e6 + 500;
bool vis[maxn];
int prime[maxn/2], cnt, a[maxn], pos[maxn];
vector<int> VI[maxn];
void sieve() {
    cnt = 0;
    for(int i = 2; i < maxn; i++) {
        if(!vis[i]) {
            prime[cnt++] = i;
            VI[i].push_back(i);
            for(int j = 2; j * i < maxn; j++) {
                vis[j * i] = 1;
                VI[j * i].push_back(i);
            }
        }
    }
}
int calc(int x) {
    int t = 0;
    //printf("%d", a[x]);
    for(int i = 0; i < VI[a[x]].size(); i++) {
        t = max(t, pos[VI[a[x]][i]]);
        //printf("%d ", VI[a[x]][i]);
        pos[VI[a[x]][i]] = x;
    }
    return t;
}
int main() {
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    sieve();
    int T;
    scanf("%d", &T);
    while(T--) {
        int n, ans = -1;
        scanf("%d", &n);
        memset(pos, 0, sizeof pos);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        int l = 0;
        for(int r = 1; r <= n; r++) {
            int t = calc(r);
            if(t > l) l = t;
            ans = max(ans, r - l);
            //printf("%d %d\n", l, r);
        }
        printf("%d\n", ans < 2 ? -1 : ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值