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\) 询问的时候,由于存在删除操作,不满足单调性,不能直接二分答案 我们 ...
随机推荐
- hdu1106 排序(字符串分割,strtok+sscanf)
排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- Django模型与创建管理员用户
默认情况下, 配置使用SQLite:
- Fiddler - 拦截手机请求
1. 在电脑上安装Fillder. 安装好之后的Fiddler 打开是这样的: 2. 浏览器访问http://127.0.0.1:8888/fiddler,下载证书并安装 3. 打开抓取https请求 ...
- HDU - 6441(费马大定理)
链接:HDU - 6441 题意:已知 n,a,求 b,c 使 a^n + b^n = c^n 成立. 题解:费马大定理 1.a^n + b^n = c^n,当 n > 2 时无解: 2. 当 ...
- leetcode-二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- poj 3468 (区间修改 区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions:147133 ...
- [Data Structures and Algorithms - 1] Introduction & Mathematics
References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...
- [leetcode-693-Binary Number with Alternating Bits]
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- 自测之Lesson9:时钟与信号
题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...
- Thunder团队第二周 - Scrum会议7
Scrum会议7 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...