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个愿望中挑选总时间和总金钱 ...
随机推荐
- Maven Waring : GroupId is duplicate of parent groupId 和 Version is duplicate of parent version
问题描述: 新项目在创建的时候,因为用到了分模块的,所以导致子模块的pom文件,报了 如下警告: 解决办法: 直接 Window --> Preferences --> Maven -- ...
- HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)
题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...
- [Noip模拟题]宠物之战senso
Description 众所周知,moreD的宠物已经被moreD奴役得体无完肤.这只宠物实在忍无可忍,把自己每天走魔法树的经历告诉了 自己的宠物.同时他还说明了自己爬树是多么地慢,以至于moreD每 ...
- luoguP1025+codevs 1039 数的划分 x
luoguP1025 + codevs1039 数的划分 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Des ...
- windows10 下 gcc/g++ 的安装
一.gcc的下载 网址:www.mingw.org ,点击右上方的 download installer 二.安装 打开安装程序,默认安装,弹出下列界面 找到mingw32-gcc-g++(注意cla ...
- psdash-为开发、测试人员提供简单的方法,在web界面查看服务器的运行情况(网络,带宽,磁盘,CPU), 同时可以在web界面查看日志
psdash是linux的系统信息web指示板主要由使用数据psutil——由此得名. github地址:https://github.com/Jahaja/psdash 特性 安装 开始 配置 截图 ...
- MySQL_DDL操作
简单的来说DLL就是对数据库的C(Create)R(Retrieve)U(Update)D(Delete) 1.数据库的创建 (1)创建:create database 数据库名:当数据库已经存在则会 ...
- Java缓存机制
1 Java缓存 1.1 jvm内置缓存 Java中实现缓存的方式有很多,比如用static hashMap基于内存缓存的jvm内置缓存,简单不实用,保对象的有效性和周期无法控制,容易造成内存急剧上升 ...
- Java虚拟机JVM详解
一.JVM内存管理 1.1JVM运行时数据区 1.1.1程序计数器:记录当前线程正在执行的字节码指定的地址(行号) 为什么需要它:程序容易被打断 1.1.2虚拟机栈:存储当前线程运行方法时所需要的数据 ...
- 【每日一包0002】array-first
github地址:https://github.com/ABCDdouyae... array-first 获取数组的第一项或者前几项 文档地址:https://www.npmjs.com/packa ...