codeforces--D. Make The Fence Great Again--动态规划

D. Make The Fence Great Again
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have a fence consisting of n vertical boards. The width of each board is 1. The height of the i-th board is ai. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from 2 to n, the condition ai−1≠ai holds.

Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the i-th board by 1, but you have to pay bi rubles for it. The length of each board can be increased any number of times (possibly, zero).

Calculate the minimum number of rubles you have to spend to make the fence great again!

You have to answer q independent queries.

Input
The first line contains one integer q (1≤q≤3⋅105) — the number of queries.

The first line of each query contains one integers n (1≤n≤3⋅105) — the number of boards in the fence.

The following n lines of each query contain the descriptions of the boards. The i-th line contains two integers ai and bi (1≤ai,bi≤109) — the length of the i-th board and the price for increasing it by 1, respectively.

It is guaranteed that sum of all n over all queries not exceed 3⋅105.

It is guaranteed that answer to each query will not exceed 1018.

Output
For each query print one integer — the minimum number of rubles you have to spend to make the fence great.

Example
inputCopy

3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2

outputCopy

2
9
0

Note
In the first query you have to increase the length of second board by 2. So your total costs if 2⋅b2=2.

In the second query you have to increase the length of first board by 1 and the length of third board by 1. So your total costs if 1⋅b1+1⋅b3=9.

In the third query the fence is great initially, so you don’t need to spend rubles.

题解:定义dp[maxn][3],dp[i][0]表示第i棵树不增高,dp[i][1]表示第i棵树增高一次,dp[i][2]表示第i棵树增高两次,然后从前往后遍历,逐个判断,因为针对第i棵树和第i-1棵树,可以增加第i棵树或者第i-1棵树,或者都不增加,反正所有情况都考虑保证第i-1棵树和第i棵树的高度不会相等就行。

AC代码

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N=3e5+1000;
struct Node{
	ll h,w;
}a[N];
ll dp[N][4];
int main()
{
//一定关闭输入输出流,不然会超时
	std::ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		cin>>a[i].h>>a[i].w;
		for(int i=1;i<=n;i++)
		dp[i][0]=dp[i][1]=dp[i][2]=1e18; 
		
		dp[1][0]=0;
		dp[1][1]=a[1].w;
		dp[1][2]=a[1].w*2;
		for(int i=2;i<=n;i++)
		{
			for(int j=0;j<3;j++)
			{
				for(int k=0;k<3;k++)
				{
				//保证第i棵树和第i-1棵树高度不会相等,然后开始动态规划判断求最小
					if((a[i-1].h+k)!=(a[i].h+j))
					{
			
						dp[i][j]=min(dp[i][j],dp[i-1][k]+j*a[i].w);
					}
				}
			}
		}
		//输出最小值
		cout<<min(dp[n][0],min(dp[n][1],dp[n][2]))<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值