码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again

时间:2019-09-22 21:43:48      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:def   test   ret   make   rate   ++   using   ons   pre   

题目链接:http://codeforces.com/contest/1221/problem/D

题意:给一个序列,要求修改某些位置的数字,使得这个序列的相邻的数不相等,每次修改,只能使得某个数字加一,每次修改的代价为b【i】,求最小所需的代价。

解题思路:经过简单分析,我们可以知道,每个数字最多只需要修改两次,那么我们定义dp【i】【j】使得前j个数字相邻数字不等的最小代价,且最后一个数字修改了i次。那么答案即为min{dp【0】【n】,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;
}

  

Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again

标签:def   test   ret   make   rate   ++   using   ons   pre   

原文地址:https://www.cnblogs.com/Zhi-71/p/11569072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!