AIM Tech Round (Div. 2) D. Array GCD dp
D. Array GCD
题目连接:
http://codeforces.com/contest/624/problem/D
Description
You are given array ai of length n. You may consecutively apply two operations to this array:
remove some subsegment (continuous subsequence) of length m < n and pay for it m·a coins;
change some elements of the array by at most 1, and pay b coins for each change.
Please note that each of operations may be applied at most once (and may be not applied at all) so you can remove only one segment and each number may be changed (increased or decreased) by at most 1. Also note, that you are not allowed to delete the whole array.
Your goal is to calculate the minimum number of coins that you need to spend in order to make the greatest common divisor of the elements of the resulting array be greater than 1.
Input
The first line of the input contains integers n, a and b (1 ≤ n ≤ 1 000 000, 0 ≤ a, b ≤ 109) — the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.
The second line contains n integers ai (2 ≤ ai ≤ 109) — elements of the array.
Output
Print a single number — the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1.
Sample Input
3 1 4
4 2 3
Sample Output
1
Hint
题意
有n个数,你可以花费i*a去删除长度i的线段,也可以花费B去让一个数+-1,但是删除操作只能进行一次,+-1对一个数也只能操作一次
并且删除操作不能删除所有的数
问你最小花费多少,可以使得剩下的数的gcd不等于1
题解:
很显然,因为不能删除完,所以必然第一个数和最后一个数会剩下来
所以我们暴力枚举第一个数和最后一个数的质因子就好了
然后开始跑dp
显然ans = f[i] + (j-i-1)*a + g[i],f[i]是前缀只修改b的最小值,g[i]是后缀只修改b的最小值
然后我们维护一下f[i]+(i-1)*a的前缀最小值,再暴力枚举g[i]就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int a[maxn];
long long A,B;
int n;
long long ans = 1e16;
vector<int> p;
long long g1[maxn];
long long g2[maxn];
long long g3[maxn];
void f(int x)
{
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
p.push_back(i);
while(x%i==0)
x/=i;
}
}
if(x!=1)p.push_back(x);
}
void solve(int x)
{
memset(g1,0,sizeof(g1));
memset(g2,0,sizeof(g2));
memset(g3,0,sizeof(g3));
for(int i=1;i<=n;i++)
{
if(a[i]%x==0)g1[i]=g1[i-1];
else if((a[i]+1)%x==0||(a[i]-1)%x==0)g1[i]=g1[i-1]+B;
else g1[i]=1e16;
}
for(int i=n;i>=1;i--)
{
if(a[i]%x==0)g2[i]=g2[i+1];
else if((a[i]+1)%x==0||(a[i]-1)%x==0)g2[i]=g2[i+1]+B;
else g2[i]=1e16;
}
g3[0]=1e16;
for(int i=1;i<=n;i++)
{
g3[i]=g1[i]-(i+1)*A;
g3[i]=min(g3[i],g3[i-1]);
}
for(int i=1;i<=n;i++)
{
ans=min(ans,g2[i]+(i-1)*A);
ans=min(ans,g1[i]+(n-i)*A);
}
for(int i=1;i<=n+1;i++)
ans=min(ans,g3[i-1]+g2[i]+A*i);
}
int main()
{
scanf("%d%lld%lld",&n,&A,&B);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=-1;i<=1;i++)
f(a[1]+i),f(a[n]+i);
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
for(int i=0;i<p.size();i++)
solve(p[i]);
printf("%lld\n",ans);
}
AIM Tech Round (Div. 2) D. Array GCD dp的更多相关文章
- Codeforces AIM Tech Round (Div. 2)
这是我第一次完整地参加codeforces的比赛! 成绩 news standings中第50. 我觉这个成绩不太好.我前半小时就过了前三题,但后面的两题不难,却乱搞了1.5h都没有什么结果,然后在等 ...
- AIM Tech Round (Div. 1) D. Birthday 数学 暴力
D. Birthday 题目连接: http://www.codeforces.com/contest/623/problem/D Description A MIPT student named M ...
- AIM Tech Round (Div. 2)——ABCD
http://codeforces.com/contest/624 A.python是用来写div2的AB题的... a, b, x, y = map(float, raw_input().split ...
- AIM Tech Round (Div. 2) C. Graph and String 二分图染色
C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...
- AIM Tech Round (Div. 2) B. Making a String 贪心
B. Making a String 题目连接: http://codeforces.com/contest/624/problem/B Description You are given an al ...
- AIM Tech Round (Div. 2) A. Save Luke 水题
A. Save Luke 题目连接: http://codeforces.com/contest/624/problem/A Description Luke Skywalker got locked ...
- AIM Tech Round (Div. 2) B
B. Making a String time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- AIM Tech Round (Div. 2) A
A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- AIM Tech Round (Div. 1) C. Electric Charges 二分
C. Electric Charges 题目连接: http://www.codeforces.com/contest/623/problem/C Description Programmer Sas ...
随机推荐
- 使用Spring MVC统一异常处理实战
1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...
- Visual Studio下Qt调用IDL
一.简单介绍: 1.ActiveQt包含QAxContainer和QAxServer组件. 1) QAxContainer允许使用COM对象,并且可以将ActiveX控件嵌入到Qt程序中去. QAxC ...
- CSS的优先级规则
CSS的优先级规则有两类 1.位置群组规则 最高优先级为元素内嵌的style样式,如<div style=” “></div> 次高优先级为html头部中的<style& ...
- iOS学习笔记之回调(一)
什么是回调 看了好多关于回调的解释的资料,一开始总觉得这个概念理解起来有点困难,可能是因为自己很少遇到这种类型的调用吧.探索良久之后,才算有点启发,下面是自己的一点理解. 我们知道,在OSI网络七层模 ...
- 序列for循环语句
序列for循环语句 序列for循环语句允许重复遍历一组序列,而这组序列可以是任何可以重复遍历的序列,如由begin()和end()函数定义的STL序列.所有的标准容器都可用作这种序列,同时它也同样可以 ...
- 【LeetCode】189 - Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- 闭包在python中的应用,translate和maketrans方法详解
python对字符串的处理是比较高效的,方法很多.maketrans和translate两个方法被应用的很多,但是具体怎么用常常想不起来. 让我们先回顾下这两个方法吧: 1.s.translate(t ...
- manacher算法_求最长回文子串长度
很好的总结,转自: http://blog.csdn.net/dyx404514/article/details/42061017 总结为:两大情况,三小情况. 两大情况:I. i <= p 1 ...
- VS常用技巧
VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 C ...
- pybombs 安装
参考:https://github.com/gnuradio/pybombs 先装:pip 然后: pip install PyBOMBS 更新源: pybombs recipes add gr-re ...