make the fence great again(dp 二维)
D. Make The Fence Great Again
2 seconds
256 megabytes
standard input
standard output
You have a fence consisting of nn vertical boards. The width of each board is 11. The height of the ii-th board is aiai. 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 22 to nn, the condition ai−1≠aiai−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 ii-th board by 11, but you have to pay bibi 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 qq independent queries.
The first line contains one integer qq (1≤q≤3⋅1051≤q≤3⋅105) — the number of queries.
The first line of each query contains one integers nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of boards in the fence.
The following nn lines of each query contain the descriptions of the boards. The ii-th line contains two integers aiai and bibi (1≤ai,bi≤1091≤ai,bi≤109) — the length of the ii-th board and the price for increasing it by 11, respectively.
It is guaranteed that sum of all nn over all queries not exceed 3⋅1053⋅105.
It is guaranteed that answer to each query will not exceed 10181018.
For each query print one integer — the minimum number of rubles you have to spend to make the fence great.
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
2
9
0
In the first query you have to increase the length of second board by 22. So your total costs if 2⋅b2=22⋅b2=2.
In the second query you have to increase the length of first board by 11 and the length of third board by 11. So your total costs if 1⋅b1+1⋅b3=91⋅b1+1⋅b3=9.
In the third query the fence is great initially, so you don't need to spend rubles.
这题。。。比赛的时候居然没有做出来,嘤嘤嘤
/*
本题大意:给定一串数字,现在你要把这串数字变为相邻数字不相等
的一串数字,变化方法为:对某个位置的数+1(可加无限多次),且有一定的花费,每个
位置花费不同,问最小花费。
本题思路:
本题可以说是想到就很好写的线性dp了,因为不知道某个数字
到底要加几次,也不知道某个数字加了之后会不会影响其它数字
,我们不可能依次枚举某个数字加的次数,但是我们很清晰的知道
每个数字最多只能加两次,为什么呢?因为加入a[i] 和 a[i + 1]
相等,加入这次我们改变a[i],那么a[i] += 1;加了之后如果形成
了a[i - 1] == a[i],那么我们还可以选择其中较小的花费加1,也
就是有可能选择a[i]又加一,此时a[i] 肯定和两端的数字都不一样
所以说每个数字必定最多加两次就可以使得他和左右两边的数字都不一样。
也就是是说我们可以用记忆化搜索搞定这个题目。
我们用dp[i][j]表示第i个数字加了j次使得第i个数字之前的所有数字
不矛盾的最小花费,那么很明显。
我们用j表示第i-1个数加的次数,k表示第i个数加的次数
因此我们判断
if val[i] + k != val[i - 1] + j:
dp[i][k] = min(dp[i][k], dp[i - 1][j] + k * cost[i])
*/
#include <cstdio>
#include <cstring>
using namespace std; typedef long long ll;
const int maxn = + ;
ll inf = 1e18 + ;
int n;
ll a[maxn], b[maxn];
ll dp[maxn][]; ll min(ll a, ll b) {
return a > b ? b : a;
} int main() {
int q;
scanf("%d", &q);
while(q --) {
scanf("%d", &n);
for(int i = ; i <= n; i ++) {
for(int j = ; j < ; j ++) dp[i][j] = inf;
}
for(int i = ; i <= n; i ++) {
scanf("%lld %lld", a + i, b + i);
}
dp[][] = 0ll;
for(int i = ; i <= n; i ++) {
for(int j = ; j < ; j ++) {
for(int k = ; k < ; k ++) {
if(a[i] + j != a[i - ] + k)
dp[i][j] = min(dp[i][j], dp[i - ][k] + j * b[i]);
}
}
}
printf("%lld\n", min(min(dp[n][], dp[n][]), dp[n][]));
}
return ;
}
make the fence great again(dp 二维)的更多相关文章
- 经典DP 二维换一维
HDU 1024 Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...
- HDU 2159 FATE (DP 二维费用背包)
题目链接 题意 : 中文题不详述. 思路 : 二维背包,dp[i][h]表示当前忍耐值为i的情况下,杀了h个怪得到的最大经验值,状态转移方程: dp[i][h] = max(dp[i][h],dp[i ...
- hdu6078 Wavel Sequence dp+二维树状数组
//#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...
- dp --- 二维dp + 最大上升子序列
<传送门> 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74477 Accepted: 27574 ...
- codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)
题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...
- 洛谷P1719 最大加权矩形 (DP/二维前缀和)
题目描述也没啥好说的,就是给你个你n*n的矩形(带权),求其中最大权值的子矩阵. 首先比较好想的就是二维前缀和,n<=120,所以可以用暴力. 1 #include<bits/stdc++ ...
- bzoj 3594 [Scoi2014]方伯伯的玉米田(DP+二维BIT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3594 [题意] 给定一个n个数的序列,有K次将一个区间内的数加1的机会,问最长不下降子 ...
- POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)
题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...
- dp 二维乃至多维背包
洛谷P1855 榨取kkksc03 分析:套路是很明显的01背包,但是这时受约束的变量有两个了,这种情况下就该用多维背包了 分析方法一样的,用dp[i][j][k]表示从前i个愿望中挑选总时间和总金钱 ...
随机推荐
- MongoDB实现增删查方法
1.添加信息 public void addInfo(Infomation infomation) { try{ // TODO Auto-generated method stub //连接Mong ...
- ESP8266-Station模式--我想连上谁
Station模式又叫做站点工作模式,类似于无线终端 处于Station模式下的ESP8266,可以连接到AP.通过Station(简称为“STA”)模式,ESP8266作为客户端连接到路由的wifi ...
- 【NOIP2016提高A组模拟9.9】爬山
题目 国家一级爬山运动员h10今天获得了一张有着密密麻麻标记的地图,在好奇心的驱使下,他又踏上了去爬山的路. 对于爬山,h10有一个原则,那就是不走回头路,于是他把地图上的所有边都标记成了有向边.他决 ...
- 消息队列之--Kafak
序言 消息丢失如何解决? 解耦 异步 并行 Docker安装Kafak 1.下载镜像 # zookeeper镜像 docker pull wurstmeister/zookeeper # kafka镜 ...
- 微信小程序登录 code 40029 天坑
微信登录时 code 大坑(服务端返回如下代码) {"errcode":40029,"errmsg":"invalid code, hints: [ ...
- Oracle数据库表空间创建、添加用户并授权
--创建test表空间CREATE TABLESPACE test_data LOGGING DATAFILE '/u01/app/oracle/oradata/test/test_data.dbf' ...
- WebView:是应用程序打开web网页的UI控件后端
public class WebViewActivity extends Activity { private WebView webView; @Override protected void on ...
- 每日踩坑 2019-07-30 H5 使用 iframe 底部有白边
用个iframe累死累活的 用 js 动态计算高度, 结果明明px都对,然后却把页面滚动条也整出来了. 查看元素盒模型也一切正常. 然后仔细观察就发现是下边多了几个像素的白色边. 然后就 百度呗 以下 ...
- LeetCode_1116.打印零与奇偶数(多线程)
LeetCode_1116 LeetCode-1116.打印零与奇偶数 假设有这么一个类: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } ...
- ffplay播放YUV数据
播放器YUV系列的格式用ffplay很方便 免费的 播放NV21 ffplay -i d:/cap.yuv -pix_fmt nv21 -s 640x480 播放YUV420P ffplay -i d ...