BZOJ4897 [Thu Summer Camp2016]成绩单 【dp】
题目链接
题解
发现我们付出的代价与区间长度无关,而与区间权值范围有关
离散化一下权值
我们设\(f[l][r][x][y]\)表示区间\([l,r]\)消到只剩权值在\([x,y]\)所需最小代价
\(f[l][r][0][0]\)即为消完的最小代价
那么
\]
转移的话,贪心地取出区间两边在权值区间\([x,y]\)以内的数,剩下区间\([l',r']\)
如果剩余区间直接消去,可以直接计算
如果不一次消去,那么枚举断点转移即可
复杂度小常数\(O(n^5)\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 55,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int f[maxn][maxn][maxn][maxn],n,a,b,w[maxn],c[maxn],tot;
void cmin(int& x,int y){x = min(x,y);}
int main(){
n = read(); a = read(); b = read();
REP(i,n) c[i] = w[i] = read();
sort(c + 1,c + 1 + n); tot = 1;
for (int i = 2; i <= n; i++) if (c[i] != c[tot]) c[++tot] = c[i];
for (int i = 1; i <= n; i++) w[i] = lower_bound(c + 1,c + 1 + tot,w[i]) - c;
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++){
int mx = -INF,mn = INF;
for (int k = l; k <= r; k++)
mx = max(mx,w[k]),mn = min(mn,w[k]);
f[l][r][0][0] = a + b * (c[mx] - c[mn]) * (c[mx] - c[mn]);
for (int x = 1; x <= tot; x++)
for (int y = x; y <= tot; y++)
f[l][r][x][y] = INF;
}
for (int len = 1; len <= n; len++){
for (int l = 1; l + len - 1 <= n; l++){
int r = l + len - 1;
for (int x = 1; x <= tot; x++)
for (int y = x; y <= tot; y++){
int ll = l,rr = r;
while (ll <= r && w[ll] >= x && w[ll] <= y) ll++;
while (rr >= ll && w[rr] >= x && w[rr] <= y) rr--;
if (ll > rr) f[l][r][x][y] = 0;
else if (ll == rr) f[l][r][x][y] = a;
else {
for (int k = ll; k < rr; k++){
cmin(f[l][r][x][y],f[ll][k][x][y] + f[k + 1][rr][x][y]);
cmin(f[l][r][x][y],f[ll][k][0][0] + f[k + 1][rr][x][y]);
cmin(f[l][r][x][y],f[ll][k][x][y] + f[k + 1][rr][0][0]);
cmin(f[l][r][x][y],f[ll][rr][0][0]);
}
}
}
for (int x = 1; x <= tot; x++)
for (int y = x; y <= tot; y++)
cmin(f[l][r][0][0],a + b * (c[y] - c[x]) * (c[y] - c[x]) + f[l][r][x][y]);
}
}
printf("%d\n",f[1][n][0][0]);
return 0;
}
BZOJ4897 [Thu Summer Camp2016]成绩单 【dp】的更多相关文章
- BZOJ4897: [Thu Summer Camp2016]成绩单【DP of DP】
Description 期末考试结束了,班主任L老师要将成绩单分发到每位同学手中.L老师共有n份成绩单,按照编号从1到n的顺序叠 放在桌子上,其中编号为i的成绩单分数为w_i.成绩单是按照批次发放的. ...
- bzoj4897 [Thu Summer Camp2016]成绩单
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4897 [题解] 第一次看这题想的是f[l,r]的区间dp发现仅记录这两个好像不能转移啊 会出 ...
- 【bzoj4897】[Thu Summer Camp2016]成绩单 区间dp
题目描述 给你一个数列,每次你可以选择连续的一段,付出 $a+b\times 极差^2$ 的代价将其删去,剩余部分拼到一起成为新的数列继续进行此操作.求将原序列全部删去需要的最小总代价是多少. 输入 ...
- BZOJ.4897.[Thu Summer Camp2016]成绩单(区间DP)
BZOJ 显然是个区间DP.令\(f[l][r]\)表示全部消掉区间\([l,r]\)的最小花费. 因为是可以通过删掉若干子串来删子序列的,所以并不好直接转移.而花费只与最大最小值有关,所以再令\(g ...
- BZOJ 4897: [Thu Summer Camp2016]成绩单 动态规划
Description 期末考试结束了,班主任L老师要将成绩单分发到每位同学手中.L老师共有n份成绩单,按照编号从1到n的顺序叠 放在桌子上,其中编号为i的成绩单分数为w_i.成绩单是按照批次发放的. ...
- [BZOJ4897][THUSC2016]成绩单(DP)
4897: [Thu Summer Camp2016]成绩单 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 220 Solved: 132[Subm ...
- 【BZOJ4896】[Thu Summer Camp2016]补退选 Trie树
[BZOJ4896][Thu Summer Camp2016]补退选 Description X是T大的一名老师,每年他都要教授许多学生基础的C++知识.在T大,每个学生在每学期的开学前都需要选课,每 ...
- BZOJ 4896 :[Thu Summer Camp2016]补退选 Trie树+Vector
4896: [Thu Summer Camp2016]补退选 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 315 Solved: 97[Submi ...
- BZOJ4896 [Thu Summer Camp2016]补退选 【trie树】
题目链接 BZOJ4896 题解 \(thu\)怎么那么喜欢出\(trie\)树的题... 我们当然可以按题意模拟建\(trie\) 询问的时候,由于存在删除操作,不满足单调性,不能直接二分答案 我们 ...
随机推荐
- JS中String对象常用的方法
1. stringObject.charAt(index) 参数:index 必需,即字符在字符串中的下标. 返回值: 返回在指定位置的字符.返回的字符是长度为 1的字符串.(length属性 ...
- Windows运行机理——主程序—WinMain
Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 在windows 操作系统下,用C 或者C++来编写MS-DOS 应用 ...
- Qt-事件处理-鼠标事件
根据书中的内容,简单的实现鼠标相关的内容 源代码如下 .h #ifndef MOUSEEVENT_H #define MOUSEEVENT_H #include <QMainWindow> ...
- 【转】: 探索Lua5.2内部实现:虚拟机指令(2) MOVE & LOAD
name args desc OP_MOVE A B R(A) := R(B) OP_MOVE用来将寄存器B中的值拷贝到寄存器A中.由于Lua是register based vm,大部分的指令都是直接 ...
- [Clr via C#读书笔记]Cp12泛型
Cp12泛型 Generic: 特点 源代码保护 类型安全 清晰代码 更佳性能 Framework中的泛型 System.Collections.Generic; 开放类型,封闭类型:每个封闭类型都有 ...
- 实现Bidirectional LSTM Classifier----深度学习RNN
双向循环神经网络(Bidirectional Recurrent Neural Networks,Bi-RNN),Schuster.Paliwal,1997年首次提出,和LSTM同年.Bi-RNN,增 ...
- Apache--Override参数详解
1 AuthConfig 允许使用所有的权限指令,他们包括AuthDBMGroupFile AuthDBMUserFile AuthGroupFile AuthName AuthTypeAut ...
- psp1111
1 本周psp 2.本周进度条 3.本周累积进度图 代码累积折线图 博文字数累积折线图 4.本周PSP饼状图
- Mac OS安装Scrapy失败
报错: DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be re ...
- <T extends Comparable<? super T>>
在看Collections源代码中,看到如下代码: public static <T extends Comparable<? super T>> void sort(List ...