题目链接

BZOJ4897

题解

发现我们付出的代价与区间长度无关,而与区间权值范围有关

离散化一下权值

我们设\(f[l][r][x][y]\)表示区间\([l,r]\)消到只剩权值在\([x,y]\)所需最小代价

\(f[l][r][0][0]\)即为消完的最小代价

那么

\[f[l][r][0][0] = min\{f[l][r][x][y] + a + b(w[y] - w[x])^2\}
\]

转移的话,贪心地取出区间两边在权值区间\([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】的更多相关文章

  1. BZOJ4897: [Thu Summer Camp2016]成绩单【DP of DP】

    Description 期末考试结束了,班主任L老师要将成绩单分发到每位同学手中.L老师共有n份成绩单,按照编号从1到n的顺序叠 放在桌子上,其中编号为i的成绩单分数为w_i.成绩单是按照批次发放的. ...

  2. bzoj4897 [Thu Summer Camp2016]成绩单

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4897 [题解] 第一次看这题想的是f[l,r]的区间dp发现仅记录这两个好像不能转移啊 会出 ...

  3. 【bzoj4897】[Thu Summer Camp2016]成绩单 区间dp

    题目描述 给你一个数列,每次你可以选择连续的一段,付出 $a+b\times 极差^2$ 的代价将其删去,剩余部分拼到一起成为新的数列继续进行此操作.求将原序列全部删去需要的最小总代价是多少. 输入 ...

  4. BZOJ.4897.[Thu Summer Camp2016]成绩单(区间DP)

    BZOJ 显然是个区间DP.令\(f[l][r]\)表示全部消掉区间\([l,r]\)的最小花费. 因为是可以通过删掉若干子串来删子序列的,所以并不好直接转移.而花费只与最大最小值有关,所以再令\(g ...

  5. BZOJ 4897: [Thu Summer Camp2016]成绩单 动态规划

    Description 期末考试结束了,班主任L老师要将成绩单分发到每位同学手中.L老师共有n份成绩单,按照编号从1到n的顺序叠 放在桌子上,其中编号为i的成绩单分数为w_i.成绩单是按照批次发放的. ...

  6. [BZOJ4897][THUSC2016]成绩单(DP)

    4897: [Thu Summer Camp2016]成绩单 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 220  Solved: 132[Subm ...

  7. 【BZOJ4896】[Thu Summer Camp2016]补退选 Trie树

    [BZOJ4896][Thu Summer Camp2016]补退选 Description X是T大的一名老师,每年他都要教授许多学生基础的C++知识.在T大,每个学生在每学期的开学前都需要选课,每 ...

  8. BZOJ 4896 :[Thu Summer Camp2016]补退选 Trie树+Vector

    4896: [Thu Summer Camp2016]补退选 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 315  Solved: 97[Submi ...

  9. BZOJ4896 [Thu Summer Camp2016]补退选 【trie树】

    题目链接 BZOJ4896 题解 \(thu\)怎么那么喜欢出\(trie\)树的题... 我们当然可以按题意模拟建\(trie\) 询问的时候,由于存在删除操作,不满足单调性,不能直接二分答案 我们 ...

随机推荐

  1. [转帖]localhost与127.0.0.1的区别

    localhost与127.0.0.1的区别 https://www.cnblogs.com/hqbhonker/p/3449975.html 前段时间用PG的时候总有问题 当时没有考虑 localh ...

  2. eclipse报这个错误org.eclipse.swt.SWTError: No more handles (eclipse 和 TeamViewer 冲突)

    错误:  org.eclipse.swt.SWTError: No more handles     at org.eclipse.swt.SWT.error(SWT.java:4387)     a ...

  3. excell 导入 导出

    1.jar包 2.POIUtils工具类 package com.esstglobal.service.utils; import java.io.BufferedInputStream; impor ...

  4. Unity编辑器 - 资源修改立即写入磁盘AssetDataBase.SaveAssets()

    Unity编辑器 - 资源修改立即写入磁盘AssetDataBase.SaveAssets() 在编写编辑器时,如果需要修改Unity序列化资源(如Prefab,美术资源,ScriptableObje ...

  5. post接口_form表单上传

    上传文件的本质是浏览器读取本地文件的内容,以二进制数据方式传输到服务端,服务端新建一个文件,将获取到的数据复制到文件中 LR中上传操作可以通过web_submit_data函数实现,支持录制要点:we ...

  6. 《Effective C++》读书笔记 被你忽略的关于构造析构赋值

    如果程序员没有定义,那么编译器会默认隐式为你创建一个copy构造函数,一个copy赋值操作符,一个析构函数.另外如果你没有声明任何构造函数,编译器会为你声明一个default构造函数. 但是只有当这些 ...

  7. 通过Ajax上传文件至WebApi服务器

    https://stackoverflow.com/questions/43013858/ajax-post-a-file-from-a-form-with-axios var formData = ...

  8. mysql下分组取关联表指定提示方法,类似于mssql中的cross apply

    转至:https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results ...

  9. 手机站测试工具(node服务器)

    最近在工作中遇到手机站测试的问题,于是就写了一个node服务外加一个第三方的转二维码功能,欢迎拍砖~ 项目地址:https://github.com/finderL/webserver

  10. “Hello World!”团队第二次会议

    今天是我们团队“hello world!”团队召开的第二次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 20 ...