Topic links: http://codeforces.com/contest/1221/problem/D
Question is intended: to a sequence, to amend certain number of positions, such that the number of adjacent sequence is not equal, each modification, only such a number by one, each modification cost is b [i], find the cost of the required minimum.
Problem-solving ideas: After a simple analysis, we can know, only need to modify each number up to two times, then we define dp [i] [j] j such that the front-digit numbers ranging from an adjacent minimum cost, and the last digit i changed times. Then the answer is min {dp [n] [0], dp [1] [n], dp [2] [n]}.
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=3e5+5; const ll inf=1e18; ll dp[3][maxn]; int a[maxn],b[maxn]; int main(){ int q; scanf("%d",&q); while(q--){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d%d",&a[i],&b[i]); dp[0][i]=inf; dp[1][i]=inf; dp[2][i]=inf; } dp[0][0]=0; dp[1][0]=b[0]*1ll; dp[2][0]=b[0]*2; for(int i=1;i<n;i++){ for(int j=0;j<=2;j++){ for(int k=0;k<=2;k++){ if((a[i]+j)!=(a[i-1]+k)){ dp[j][i]=min(dp[j][i],dp[k][i-1]+1ll*j*b[i]); } } } } // for(int i=0;i<n;i++)cout<<dp[0][i]<<" "<<dp[1][i]<<" "<<dp[2][i]<<endl; printf("%lld\n",min(dp[0][n-1],min(dp[1][n-1],dp[2][n-1]))); } return 0; }