CF321E Ciel and Gondolas
题意:给定序列,将其分成k段。如果[l, r]在一段,那么每对不相同的i,j∈[l, r]都会有ai,j的代价。求最小总代价。
解:提供两种方案。第三种去bzoj贞鱼的n²算法。
决策单调性优化:
对于两个转移点j1 < j2,若在某个点i上j2更优,则i后面的j2全部更优。这就是决策单调性。
有两种写法。一种是维护决策栈(???),我自己YY了一个线段树写法WA飞了。
还有一种是分治。对于一段待转移的区间[l, r],它们的最优转移来自于[L, R]
则对于mid = (l + r) >>1,我们扫一遍[L, R],得到mid的最优转移p
然后分治下去。
#include <cstdio>
#include <cstring> const int N = , K = , INF = 0x3f3f3f3f; inline void read(int &x) {
x = ;
char c = getchar();
while(c < '' || c > '') {
c = getchar();
}
while(c >= '' && c <= '') {
x = (x << ) + (x << ) + c - ;
c = getchar();
}
return;
} int f[N][K], n, k, a[N][N];
int tag[N], turn; inline void exmin(int &a, int b) {
a > b ? a = b : ;
return;
} void solve(int L, int R, int l, int r) {
if(R < L || r < l) return;
//printf("solve %d %d -> %d %d \n", L, R, l, r);
if(L == R) {
for(int i = l; i <= r; i++) {
f[i][turn] = f[R][turn - ] + a[i][R + ];
//printf("f %d %d = %d + %d \n", i, turn, f[R][turn - 1], a[i][R + 1]);
}
return;
}
int mid = (l + r) >> , p = L;
for(int i = L; i < mid && i <= R; i++) {
if(f[mid][turn] > f[i][turn - ] + a[mid][i + ]) {
p = i;
f[mid][turn] = f[i][turn - ] + a[mid][i + ];
//printf("f %d %d = %d from %d \n", mid, turn, f[mid][turn], p);
}
}
solve(L, p, l, mid - );
solve(p, R, mid + , r);
return;
} int main() { //freopen("in.in", "r", stdin);
//freopen("my.out", "w", stdout); read(n); read(k);
for(register int i = ; i <= n; i++) {
for(register int j = ; j <= n; j++) {
read(a[i][j]);
if(i < j) a[i][j] = ;
}
}
for(register int i = ; i <= n; i++) {
for(register int j = n; j >= ; j--) {
a[i][j] += a[i][j + ];
}
}
for(register int i = ; i <= n; i++) {
for(register int j = n; j >= ; j--) {
a[i][j] += a[i - ][j];
}
}
memset(f, 0x3f, sizeof(f));
f[][] = ;
for(int j = ; j <= k; j++) {
turn = j;
//printf("turn = %d \n", j);
solve(, n - , , n);
/*for(int i = 1; i <= n; i++) {
printf("%d ", f[i][turn]);
}
printf("\n");*/
} printf("%d\n", f[n][k]);
return ;
}
决策单调性优化AC代码
带权二分:我们假装那个函数是凸的。
然后按照套路带权二分一波。
记得一定要让k的变化和D的变化同步,这一点可以通过调整±D来实现。
之后尽量让k小,l端mid,r端要mid - 1。
#include <cstdio>
#include <algorithm> typedef long long LL;
const int N = ;
LL INF = 1e17; inline void read(LL &x) {
x = ;
char c = getchar();
while(c < '' || c > '') {
c = getchar();
}
while(c >= '' && c <= '') {
x = (x << ) + (x << ) + c - ;
c = getchar();
}
return;
} int n, k, g[N];
LL f[N], a[N][N], D, ans; inline int check(LL mid) {
D = mid;
for(register int i = ; i <= n; i++) {
f[i] = INF;
for(register int j = ; j < i; j++) {
if(f[i] > f[j] + a[i][j + ] - D) {
f[i] = f[j] + a[i][j + ] - D;
g[i] = g[j] + ;
}
else if(f[i] == f[j] + a[i][j + ] - D) {
g[i] = std::min(g[i], g[j] + );
}
}
}
ans = f[n];
return g[n];
} int main() {
scanf("%d%d", &n, &k);
for(register int i = ; i <= n; i++) {
for(register int j = ; j <= n; j++) {
read(a[i][j]);
if(i < j) a[i][j] = ;
}
}
for(register int i = ; i <= n; i++) {
for(register int j = n; j >= ; j--) {
a[i][j] += a[i][j + ];
}
}
for(register int i = ; i <= n; i++) {
for(register int j = n; j >= ; j--) {
a[i][j] += a[i - ][j];
}
}
// LL l = -a[n][], r = a[n][];
while(l < r) {
LL mid = (l + r + ) >> ;
int t = check(mid);
if(t == k) {
printf("%lld\n", ans + k * mid);
return ;
}
if(t < k) l = mid;
else r = mid - ;
}
check(r);
printf("%lld\n", ans + k * r);
return ;
}
带权二分AC代码
CF321E Ciel and Gondolas的更多相关文章
- CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性
LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...
- CF321E Ciel and Gondolas 【决策单调性dp】
题目链接 CF321E 题解 题意:将\(n\)个人分成\(K\)段,每段的人两两之间产生代价,求最小代价和 容易设\(f[k][i]\)表示前\(i\)个人分成\(k\)段的最小代价和 设\(val ...
- [CF321E]Ciel and Gondolas&&[BZOJ5311]贞鱼
codeforces bzoj description 有\(n\)个人要坐\(k\)辆车.如果第\(i\)个人和第\(j\)个人同坐一辆车,就会产生\(w_{i,j}\)的代价. 求最小化代价.\( ...
- CF321E Ciel and Gondolas & BZOJ 5311 贞鱼
一眼可以看出$O(kn^{2})$的$dp$方程,然后就不会了呜呜呜. 设$f_{i, j}$表示已经选到了第$i + 1$个数并且选了$j$段的最小代价,那么 $f_{i, j} = f_{p, j ...
- 【wqs二分 || 决策单调性】cf321E. Ciel and Gondolas
把状态看成层,每层决策单调性处理 题目描述 题目大意 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半.这不?他们遇到了大麻烦!n只贞鱼到陆地上乘车,现在有k辆汽车可以租用.由于贞鱼们 ...
- 【BZOJ5311/CF321E】贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性)
[BZOJ5311/CF321E]贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性) 题面 BZOJ CF 洛谷 辣鸡BZOJ卡常数!!!!!! 辣鸡BZOJ卡常数!!!!!! ...
- 【23.58%】【code forces 321E】Ciel and Gondolas
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【CodeForces】【321E】Ciel and Gondolas
DP优化/四边形不等式 这题……跟邮局那题简直一模一样吧……好水的E题…… 设dp[i][j]表示前 i 艘“gondola”坐了前 j 个人,那么方程即为$dp(i,j)=min\{ dp[i-1] ...
- CodeForces - 321E:Ciel and Gondolas (四边形不等式优化DP)
题意:N个人排成一行,分成K组,要求每组的不和谐值之和最小. 思路:开始以为是斜率优化DP,但是每个区间的值其实已经知道了,即是没有和下标有关的未知数了,所以没必要用斜率. 四边形优化. dp[i][ ...
随机推荐
- 【nodejs】让nodejs像后端mvc框架(asp.net mvc )一样处理请求--控制器的声明定义和发现篇(3/8)
文章目录 前情概要 前面文章把路由已经介绍的差不多了,包括url映射,路由选择等.接下来讲一讲controller的一些基本规则 BaseController的所有代码都在这里拉.相当简单. 主要逻辑 ...
- 作业20171130 final发布 成绩
申诉 对成绩有疑问或不同意见的同学,请在群里[@杨贵福]. 申诉时间截止2017年12月16日 17:00. 更新 第一周和第二周成绩分别应占比20%和10%,计算时刚好反了.所以同学们的最终成绩有变 ...
- 个人阅读作业WEEK7 (软件工程的瀑布, 大泥球, 教堂,集市,和银弹)
一 . 关于银弹 (Silver Bullet) 银弹,被引申为解决问题的有效办法.IBM大型机之父福瑞德·布鲁克斯在1986年的论文<没有银弹>中表达了他的观点:软件工程中不存在银弹—— ...
- nginx+tpmcat+redis实现session共享
nginx+tpmcat+redis实现session共享 版本:nginx nginx-1.8.0.tar.gztomcat apache-tomcat-7.0.78.tar.gzredis re ...
- python 使用read_csv读取 CSV 文件时报错
读取csv文件时报错 df = pd.read_csv('c:/Users/NUC/Desktop/成绩.csv' ) Traceback (most recent call last): File ...
- weex 开发踩坑日记--环境配置、安卓运行、adb、开发
环境配置方面 1.需要安装java和android环境,java的话一定要下载jdk而不是jre. 在"系统变量"新建一个变量名为JAVA_HOME的变量,变量值为你本地java的 ...
- css3 @media 实现响应式布局
使用css3的@media,可以实现针对不同媒体.不同分辨率的响应式布局. 方法1:根据不同分辨率使用不同css文件 <link rel="stylesheet" media ...
- 手动安装ettercap的过程
知乎推送了一个中间人攻击的软件 ettercap 想着尝试进行一下安装学习, 如果有机会的话安全测试部分应该用的到. 1. 下载: wget https://codeload.github.com/E ...
- panda迭代
1.注意 - 不要尝试在迭代时修改任何对象.迭代是用于读取,迭代器返回原始对象(视图)的副本,因此更改将不会反映在原始对象上. 2.itertuples()方法将为DataFrame中的每一行返回一个 ...
- python代码格式检查工具部署pre_commit
如何使用pre_commit?1. 合并该分支 2. 在git根目录下使用pre-commit install即可3. 如果没有装pre-commit 安装一下pip install pre-comm ...