题目链接

CF321E

题解

题意:将\(n\)个人分成\(K\)段,每段的人两两之间产生代价,求最小代价和

容易设\(f[k][i]\)表示前\(i\)个人分成\(k\)段的最小代价和

设\(val(i,j)\)为\(i\)到\(j\)两两之间产生的代价和,容易发现就是一个矩形,可以预处理前缀和\(O(1)\)计算

那么有

\[f[k][i] = min\{f[k - 1][j] + val(j + 1,i)\}
\]

直接转移显然\(O(n^2k)\)

我们把\(val(j + 1,i)\)拆开,也不能分离\(i\)和\(j\)

很好,可以大胆猜想这个\(dp\)是符合决策单调性的

证明:

如果对于\(x > y\)且\(x\)作为\(i\)的决策,一定有

\[f[k - 1][x] + val(x + 1,i) \le f[k - 1][y] + val(y + 1,i)
\]

那么对于\(i' > i\),由几何面积可以得知\(val(x + 1,i') - val(x + 1,i) \le val(y + 1,i') - val(y + 1,i)\)

所以仍有

\[f[k - 1][x] + val(x + 1,i') \le f[k - 1][y] + val(y + 1,i')
\]

故对于\(i\)决策时比\(y\)更优的\(x\),在\(i' > i\)的决策时依旧更优

即该\(dp\)满足决策单调性

证毕

所以用一个队列维护三元组,即可做到\(O(n^2 + knlogn)\)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define REP(i,n) for (register 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 = 4005,maxm = 805,INF = 0x3f3f3f3f;
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;
}
struct tri{int l,r,pos;}q[maxn];
int head,tail;
int s[maxn][maxn],f[maxm][maxn],n,K,now;
inline int val(int i,int j){
return s[i][i] - s[i][j - 1] - s[j - 1][i] + s[j - 1][j - 1];
}
inline bool check(int pos,int i,int j){
return f[now - 1][i] + val(pos,i + 1) <= f[now - 1][j] + val(pos,j + 1);
}
inline void work(){
f[now][0] = INF;
q[head = tail = 0] = (tri){1,n,0};
tri u;
for (register int i = 1; i <= n; i++){
u = q[head];
f[now][i] = f[now - 1][u.pos] + val(i,u.pos + 1);
q[head].l++;
if (q[head].l > q[head].r) head++;
while (head <= tail){
u = q[tail--];
if (check(u.l,i,u.pos)){
if (head > tail) {q[++tail] = (tri){u.l,n,i}; break;}
continue;
}
else if (!check(u.r,i,u.pos)){
q[++tail] = u;
if (u.r == n) break;
q[++tail] = (tri){u.r + 1,n,i};
break;
}
else {
int l = i + 1,r = n,mid;
while (l < r){
mid = l + r >> 1;
if (check(mid,i,u.pos)) r = mid;
else l = mid + 1;
}
q[++tail] = (tri){u.l,l - 1,u.pos};
q[++tail] = (tri){l,n,i};
break;
}
}
}
}
int main(){
n = read(); K = read();
REP(i,n) REP(j,n) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + read();
REP(i,n) f[1][i] = val(i,1); f[1][0] = INF;
for (now = 2; now <= K; now++) work();
printf("%d\n",f[K][n] >> 1);
return 0;
}

CF321E Ciel and Gondolas 【决策单调性dp】的更多相关文章

  1. CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性

    LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...

  2. 【wqs二分 || 决策单调性】cf321E. Ciel and Gondolas

    把状态看成层,每层决策单调性处理 题目描述 题目大意 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半.这不?他们遇到了大麻烦!n只贞鱼到陆地上乘车,现在有k辆汽车可以租用.由于贞鱼们 ...

  3. BZOJ4426 :最大生产率(贪心+决策单调性DP)

    题意:给出N个人,现在让你分P组,每组的工作效率是最小结束时间-最大开始时间,要求每一组的效率的正数,求最大效率和.N<1000 思路: 把包含至少一个其他的分到A组:否则到B组. A组的要么单 ...

  4. CF321E Ciel and Gondolas

    题意:给定序列,将其分成k段.如果[l, r]在一段,那么每对不相同的i,j∈[l, r]都会有ai,j的代价.求最小总代价. 解:提供两种方案.第三种去bzoj贞鱼的n²算法. 决策单调性优化: 对 ...

  5. [CF321E]Ciel and Gondolas&&[BZOJ5311]贞鱼

    codeforces bzoj description 有\(n\)个人要坐\(k\)辆车.如果第\(i\)个人和第\(j\)个人同坐一辆车,就会产生\(w_{i,j}\)的代价. 求最小化代价.\( ...

  6. BZOJ2216 [Poi2011]Lightning Conductor 【决策单调性dp】

    题目链接 BZOJ2216 题解 学过高中数学都应知道,我们要求\(p\)的极值,参变分离为 \[h_j + sqrt{|i - j|} - h_i \le p\] 实际上就是求\(h_j + sqr ...

  7. 洛谷 P3515 [ POI 2011 ] Lightning Conductor —— 决策单调性DP

    题目:https://www.luogu.org/problemnew/show/P3515 决策单调性... 参考TJ:https://www.cnblogs.com/CQzhangyu/p/725 ...

  8. LOJ2074/2157 JSOI2016/POI2011 Lightning Conductor 决策单调性DP

    传送门 我们相当于要求出\(f_i = \max\limits_{j=1}^{n} (a_j + \sqrt{|i-j|})\).这个绝对值太烦人了,考虑对于\(i>j\)和\(i<j\) ...

  9. Wannafly Camp 2020 Day 3F 社团管理 - 决策单调性dp,整体二分

    有 \(n\) 个数构成的序列 \({a_i}\),要将它划分为 \(k\) 段,定义每一段的权值为这段中 \((i,j) \ s.t. \ i<j,\ a_i=a_j\) 的个数,求一种划分方 ...

随机推荐

  1. windows上的mysql配置过程

    个人电脑的mysql配置,记录下来留作备忘 1. 首先去官网下载最新的mysql安装包,我下的是5.7.25,地址是 https://dev.mysql.com/downloads/windows/ ...

  2. python 网页转pdf

    主要使用的是wkhtmltopdf的Python封装——pdfkit centos环境 安装:Install python-pdfkit pip install pdfkit 安装:Install w ...

  3. kubernetes dashboard 安装时出现9090: getsockopt: connection refused错误

    转载于:https://blog.csdn.net/lucy06/article/details/79082302 安装kubernetes  dashboard时,出现错误: Error: 'dia ...

  4. leetcode第217.题存在重复元素

    1.题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 2.示例 2.1 输入: [1,2,3,1 ...

  5. SQL IF while 游标

    -- if语句使用示例 declare @a int set @a=1 begin print @a =@a+1 end else begin print 'noooo' end -- while语句 ...

  6. [cmake] Basic Tutorial

    Basic Project The most basic porject is an executable built from source code file. CMakeLists.txt cm ...

  7. Vue 实例详解与生命周期

    Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...

  8. “Hello World!”团队第六周第六次会议

    “Hello World!”团队第六周第六次会议   博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.checkout& ...

  9. 文件上传到tomcat服务器 commons-fileupload的详细介绍与使用

    三个类:DiskFileUpload.FileItem和FileUploadException.这三个类全部位于org.apache.commons.fileupload包中. 首先需要说明一下for ...

  10. Delphi中BCD和Currency类型

    用了这些年的Delphi,竟然对Currency及TBCDField一知半解,下文给了很好的讲解,值得一读. 一.       BCD类型 BCD即Binary-Coded Decimal?,在Del ...