【dp】CodeForces - 623B Array GCD
Link:http://codeforces.com/problemset/problem/623/B
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")
/*
CodeForces - 623B
題意:給出n個數的序列,對序列可以有兩種操作:
1. 將一段len長的序列直接刪去,消耗len*a的能量; 一個序列只能刪一次,並且不能刪完整個序列。
2. 將一個數+1或-1,消耗b能量; 每個數只能變一次。
使得序列的最大公約數大於1,消耗的能量最小值
題解:因為刪去的部分不會同時包含第一個數a[1]和最後一個數a[n],那麼序列的最大公約數的可能性為:
a[1]-1,a[1],a[1]+1,a[n]-1,a[n],a[n]+1 的質因子。據說一個數n的質因子個數為log2(n)的規模,那麼其實
最大公約數的可能值很小。
對於已知的最大公約數,計算序列最小花費,dp如下定義:
LL dp[N][5]; //0 不處理,1 處理+1-1,2 刪去,3 刪去用過不處理, 4 刪去用過處理-1+1
*/
const double PI = acos(-1.0);
const double eps = 1e-6;
//const int INF=0x3f3f3f3f;
const LL INF = 1e18;
const int Mod = 1e9;
const int N = 1000010;
int a[N];
set<int> gcd;
void prime(int x)
{
for(int i = 2; i*i <= x; i++){
if(x%i == 0)
gcd.insert(i);
while(x%i==0)
x /= i;
}
if(x != 1)
gcd.insert(x);
}
int n,aa,bb;
LL dp[N][5]; //0 不處理,1 處理+1-1,2 刪去,3 刪去用過不處理, 4 刪去用過處理-1+1
LL solve(int x)
{
for(int i = 1; i <= n; i++)
for(int j = 0; j < 5; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for(int i = 1; i <= n; i++){
if(a[i]%x == 0)
{
dp[i][0] = min(dp[i-1][0],dp[i-1][1]);
dp[i][3] = min(dp[i-1][2],min(dp[i-1][3],dp[i-1][4]));
}
if((a[i]-1)%x==0 || (a[i]+1)%x==0)
{
dp[i][1] = min(dp[i-1][0],dp[i-1][1])+bb;
dp[i][4] = min(dp[i-1][2],min(dp[i-1][3],dp[i-1][4]))+bb;
}
dp[i][2] = min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+aa;
}
LL mi = INF;
for(int i = 0; i < 5; i++)
mi = min(mi,dp[n][i]);
//printf("%d\n",mi);
return mi;
}
int main()
{
while(~scanf("%d%d%d",&n,&aa,&bb))
{
gcd.clear();
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
prime(a[1]);
prime(a[1]+1);
prime(a[1]-1);
prime(a[n]);
prime(a[n]+1);
prime(a[n]-1);
LL mi = INF;
set<int>::iterator it;
for(it = gcd.begin(); it != gcd.end(); it++){
//printf("gcd = %d\n",*it);
mi = min(mi,solve(*it));
}
printf("%lld\n",mi);
}
return 0;
}
相關文章
- Codeforces1493D GCD of an Array3DGC
- BZOJ3853 : GCD ArrayGC
- Codeforces Sereja and Array
- codeforces 148 D 概率dp
- codeforces455A Boredom (裸DP)
- Codeforces 372B Counting Rectangles is Fun:dp套dp
- Codeforces Round #323 (Div. 2) C gcdGC
- CodeForces - 628D (數位dp)
- Codeforces 180C Letter:dp
- Educational Codeforces Round 19 E. Array Queries
- Codeforces 158E Phone Talks:dp
- Codeforces 432D Prefixes and Suffixes:KMP + dpKMP
- Codeforces 294B Shaass and Bookshelf:dp
- 【dp】codeforces 837D Round Subset
- 【dp】codeforces 830-A Office Keys
- codeforces 148 D Bag of mice(概率dp)
- CodeForces 401D 數位DP
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
- 【數位dp】Beautiful numbers CodeForces - 55D
- Codeforces Round #358 (Div. 2) D dp
- Codeforces 459E Pashmak and Graph:dp + 貪心
- Codeforces 148D Bag of mice (概率dp)
- Codeforces Round #316 (Div. 2) E dp
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces 461B Appleman and Tree:Tree dpAPP
- Codeforces 366C Dima and Salad:揹包dp
- Codeforces 478D Red-Green Towers:dp
- codeforces 341C Iahub and Permutations(組合數dp)
- Puzzles CodeForces 696B 樹形DP 期望計算
- Codeforces Round #336 (Div. 2) D 區間dp
- Codeforces 245H Queries for Number of Palindromes:區間dp
- Codeforces 455B A Lot of Games:博弈dp【多局遊戲】GAM遊戲
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Arraydev
- Codeforces Round #336 (Div. 2) C 二分+dp
- Codeforces 219D Choosing Capital for Treeland:Tree dpAPI
- Codeforces 429B Working out:dp【列舉交點】
- Codeforces 571B Minimization:dp + 貪心【前後相消】
- Codeforces 11D A Simple Task 題解 [ 藍 ] [ 狀壓 dp ]