[CF321E]Ciel and Gondolas&&[BZOJ5311]贞鱼
description
有\(n\)个人要坐\(k\)辆车。如果第\(i\)个人和第\(j\)个人同坐一辆车,就会产生\(w_{i,j}\)的代价。
求最小化代价。\(n\le4000\)
sol
凸优化+决策单调性优化
这么一讲其实这题就已经做完了,复杂度\(O(n\log n\log w)\)
code
\(bzoj\)上需要卡常。上网蒯个读入优化模板就行了。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 4005;
struct node{int j,l,r;}q[N];
int n,k,s[N][N],f[N],g[N],hd,tl;
int cal(int j,int i){
return f[j]+(s[i][i]-s[i][j]-s[j][i]+s[j][j]>>1);
}
bool better(int i,int j,int k){
int si=cal(i,k),sj=cal(j,k);
return si<sj||(si==sj&&g[i]<g[j]);
}
int binary(int i,int j){
int l=q[tl].l,r=n,res=0;
while (l<=r){
int mid=l+r>>1;
if (better(i,j,mid)) res=mid,r=mid-1;
else l=mid+1;
}
return res;
}
void solve(int c){
q[hd=tl=1]=(node){0,0,n};
for (int i=1;i<=n;++i){
++q[hd].l;if (q[hd].l>q[hd].r) ++hd;
f[i]=cal(q[hd].j,i)+c;g[i]=g[q[hd].j]+1;
if (hd>tl||better(i,q[tl].j,n)){
while (hd<=tl&&better(i,q[tl].j,q[tl].l)) --tl;
if (hd>tl) q[++tl]=(node){i,i,n};
else{
int x=binary(i,q[tl].j);
q[tl].r=x-1;q[++tl]=(node){i,x,n};
}
}
}
}
int main(){
n=gi();k=gi();
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+gi();
int l=0,r=s[n][n],res=0;
while (l<=r){
int mid=l+r>>1;solve(mid);
if (g[n]<=k) res=mid,r=mid-1;else l=mid+1;
}
solve(res);printf("%d\n",f[n]-k*res);return 0;
}
[CF321E]Ciel and Gondolas&&[BZOJ5311]贞鱼的更多相关文章
- CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性
LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...
- CF321E Ciel and Gondolas
题意:给定序列,将其分成k段.如果[l, r]在一段,那么每对不相同的i,j∈[l, r]都会有ai,j的代价.求最小总代价. 解:提供两种方案.第三种去bzoj贞鱼的n²算法. 决策单调性优化: 对 ...
- 【wqs二分 || 决策单调性】cf321E. Ciel and Gondolas
把状态看成层,每层决策单调性处理 题目描述 题目大意 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半.这不?他们遇到了大麻烦!n只贞鱼到陆地上乘车,现在有k辆汽车可以租用.由于贞鱼们 ...
- CF321E Ciel and Gondolas & BZOJ 5311 贞鱼
一眼可以看出$O(kn^{2})$的$dp$方程,然后就不会了呜呜呜. 设$f_{i, j}$表示已经选到了第$i + 1$个数并且选了$j$段的最小代价,那么 $f_{i, j} = f_{p, j ...
- BZOJ5311 贞鱼(动态规划+wqs二分+决策单调性)
大胆猜想答案随k变化是凸函数,且有决策单调性即可.去粘了份fread快读板子才过. #include<iostream> #include<cstdio> #include&l ...
- CF321E Ciel and Gondolas 【决策单调性dp】
题目链接 CF321E 题解 题意:将\(n\)个人分成\(K\)段,每段的人两两之间产生代价,求最小代价和 容易设\(f[k][i]\)表示前\(i\)个人分成\(k\)段的最小代价和 设\(val ...
- bzoj5311: 贞鱼
还是年轻啊算的时候少乘一个4000被卡二分上界了...%%%%bright教我超级快速读D飞bzoj垃圾卡常数据 我们容易写出这样的DP方程:f[i][j]=f[k][j-1]+val(k+1,j) ...
- 【BZOJ5311/CF321E】贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性)
[BZOJ5311/CF321E]贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性) 题面 BZOJ CF 洛谷 辣鸡BZOJ卡常数!!!!!! 辣鸡BZOJ卡常数!!!!!! ...
- BZOJ5311,CF321E 贞鱼
题意 Problem 5311. -- 贞鱼 5311: 贞鱼 Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 677 Solved: 150[Subm ...
随机推荐
- Django RF:学习笔记(8)——快速开始
Django RF:学习笔记(8)——快速开始 安装配置 1.使用Pip安装Django REST Framework: pip install djangorestframework 2.在Sett ...
- 11. Container With Most Water(装最多的水 双指针)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Educational Codeforces Round 59 Solution
A. Digits Sequence Dividing 签. #include <bits/stdc++.h> using namespace std; #define N 1010 ch ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON DispArc
zw版[转发·台湾nvp系列Delphi例程]HALCON DispArc zw版[转发·台湾nvp系列Delphi例程]HALCON DispArc----------RAD Studio XE D ...
- STM32端口输入输出模式配置
STM32的IO口模式配置 根据数据手册提供的信息,stm32的io口一共有八种模式,他们分别是: 四种输入模式 上拉输入:通过内部的上拉电阻将一个不确定的信号通过一个电阻拉到高电平. 下拉输入:把电 ...
- js的重载
1.重载 //重载(个数不同,类型不同)function prop(){var firstP = document.getElementById("p");if(arguments ...
- CSU 1963 Feed the rabbit(斜率优化dp)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1963 题意:有m个坑,每只兔子会在ti时刻回到坑中,现在有n个人,每个人都可以从任意时间(&l ...
- c++ 重设容器的长度(resize)
#include <iostream> #include <vector> using namespace std; int main () { vector<int&g ...
- shell 判断一个字符串是否为空
test.sh #!/bin/bash echo "enter the string:" read filename if test $filename ; then echo & ...
- 使用dos2unix批量转换文件
使用dos2unix批量转换文件 dos2unix是Linux下的一个用户转换格式的程序,由于windows上文件的结束符和linux上的不同,那么在windows上编写的文件或者是脚本在Linux上 ...