GCD + st表 + 二分

Problem - 1632D - Codeforces

题意

给出一个长度为 \(n\;(1<=n<=2*10^5)\) 的数组 \(a[i]\;(1<=a[i]<=10^9)\), 可以修改任何一个位置的数为任何一个正整数,对于任意一段区间 \([l,r]\;(1<=l<=r<=n)\),不能出现 \(gcd(a[l],a[l+1],...,a[r])=r-l+1\)

对于每个 \(i(1<=i<=n)\) , 求把前 i 个数组成的数组修改好的最小操作次数

思路

  1. 每次最优的修改是把当前的数改为一个极大的质数,这样包含这个数的区间肯定都是合法的

  2. 记上一次修改的位置是 L,对于每一个右端点 r,从 L + 1 到 r 枚举左端点,逐个判断 \([l,r]\) 是否合法,有不合法的就把 \(a[r]\) 改为大质数并更新 L

  3. 上述策略是 \(O(n^2)\) 的,但固定右端点,枚举左端点的过程是有单调性的,因为随着区间长度变小,区间gcd变大,因此可以二分找到

    区间gcd == 区间长度的位置

  4. 区间gcd用st表预处理出

#include <bits/stdc++.h>
using namespace std;
#define endl "\n" typedef long long ll;
typedef pair<int, int> PII; const int N = 2e5 + 10;
int n;
int a[N];
template<typename T> struct ST
{
ST(T a[], int n){
siz = n;
g.resize(n+1);
int t = __lg(n) + 1;
for(int i=1;i<=n;i++) g[i].resize(t); for(int i = 1; i <= n; i++) g[i][0] = a[i];
for(int j = 1; j < t; j++)
{
for(int i = 1; i <= n - (1<<j)+1; i++)
{
g[i][j] = __gcd(g[i][j-1], g[i+(1 << (j-1))][j-1]);
}
}
}
T get_gcd(int l,int r)
{
int k = __lg(r-l+1);
return __gcd(g[l][k], g[r-(1<<k)+1][k]);
}
private:
int siz = 0;
vector<vector<T>> g;
}; int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
ST<int> st(a, n);
int cnt = 0;
int L = 0;
for (int i = 1; i <= n; i++)
{
int l = L, r = i;
while(l + 1 != r)
{
int mid = l + r >> 1;
if (st.get_gcd(mid, i) >= i - mid + 1)
r = mid;
else
l = mid;
}
int tmp = st.get_gcd(r, i);
if (tmp == i - r + 1)
{
cnt++;
L = i;
}
cout << cnt << " ";
}
cout << endl;
return 0;
}

Codeforces Round #769 (Div. 2) - D. New Year Concert的更多相关文章

  1. Codeforces Round #769 (Div. 2)D,E

    D.New Year Concert 传送门 题目大意: 一个长为 N ( 1 ≤ N ≤ 2 × 1 0 5 ) N(1\leq N\leq2\times 10^5) N(1≤N≤2×105)的序列 ...

  2. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  10. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

随机推荐

  1. 回归分析 3.X 多元线性回归

    多元线性回归模型 参数估计 模型表示 我们先将模型 \[y_{i}=\beta_{0}+\beta_{1} x_{i 1}+\cdots+\beta_{p} x_{i k}+\epsilon_{i}, ...

  2. linux中磁盘如何由dos格式怎么变为gpt格式

    一般情况下,我们进行磁盘分区管理使用gdisk命令比较方便快捷,但假如我们想要大于2T的磁盘使用fdisk命令已经无法使用,此刻我们该怎么办?这时我们可以使用parted命令来把磁盘转换为gpt格式, ...

  3. C++数组(二):二维数组

    二维数组 什么是二维数组?二维数组就是在一维数组的基础上增加一个维度. 二维数组的定义方式 数据类型 数组名[行数][列数]; int arr[2][3]; arr[0][0] = 1; arr[0] ...

  4. cypress初探

    long long ago就已经被各大公众号洗脑这款神奇的工具,那我们一起来学习下吧(基础入门安装,边学习边记录,勿喷谢谢) 第一步:访问官方网站:https://www.cypress.io/ 第二 ...

  5. springboot Elasticsearch 实体创建索引设置Date 类型字段失败

    springboot Elasticsearch 实体创建索引设置Date 类型字段失败,需添加以下注解 @Field(type = FieldType.Date, format = DateForm ...

  6. Flink RocksDB参数调优说明

    参数名 说明 state.backend.rocksdb.block.blocksize block 的大小,默认值为4KB.在生产环境中总是会适当调大一些,一般32KB比较合适,对于机械硬盘可以再增 ...

  7. http如何全站301重定向到https

    对于301重定向这一概念玩SEO的同志们都不陌生了,近些年来https协议越来越火,谷歌已经明确了使用https相对http来说会有更好的排名,再加上百度大大已经明确了对https的扶持政策,老威现在 ...

  8. Web框架-inoic

    ionic 下载ionic 使用命令行安装npm 1.安装node.js 最后安装的目录在D:\2019-10-14\2019-11-04-2 检测nodejs安装成功? node -v 如果报错,是 ...

  9. Win10删除此电脑默认的7个文件夹

    删除方法 用记事本拷贝以下内容,改文件后缀为reg,然后点击执行. Windows Registry Editor Version 5.00 ;如需还原去除上语句前减号即可 ;取消我的电脑" ...

  10. 使用vue渲染大量数据时应该怎么优化?

    Object.freeze 适合一些 big data的业务场景.尤其是做管理后台的时候,经常会有一些超大数据量的 table,或者一个含有 n 多数据的图表,这种数据量很大的东西使用起来最明显的感受 ...