zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again

    题目链接: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
    ",min(dp[0][n-1],min(dp[1][n-1],dp[2][n-1])));
    	}
        return 0;
    }
    

      

  • 相关阅读:
    图算法之广度优先遍历
    图形算法之深度优先遍历
    list下SORT排序方法使用
    Linux使用curl进行接口测试
    Template方法应用
    profile[计算方法耗时模块]用法
    性能测试的实施及总结(二)
    yum源配置
    Dockerfile文件
    Docker的Image与Container
  • 原文地址:https://www.cnblogs.com/Zhi-71/p/11569072.html
Copyright © 2011-2022 走看看