BZOJ5311 贞鱼(动态规划+wqs二分+决策单调性)
大胆猜想答案随k变化是凸函数,且有决策单调性即可。去粘了份fread快读板子才过。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 4010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=;char c=getchar();
while (c<''||c>'') c=getchar();
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x;
}
struct FastIO {
static const int S = 1e7;
int wpos;
char wbuf[S];
FastIO() : wpos() {}
inline int xchar() {
static char buf[S];
static int len = , pos = ;
if (pos == len)
pos = , len = fread(buf, , S, stdin);
if (pos == len) exit();
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = ;
while (c <= ) c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x;
}
inline int xint()
{
int s = , c = xchar(), x = ;
while (c <= ) c = xchar();
if (c == '-') s = -, c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x * s;
}
inline void xstring(char *s)
{
int c = xchar();
while (c <= ) c = xchar();
for (; c > ; c = xchar()) * s++ = c;
*s = ;
}
inline void wchar(int x)
{
if (wpos == S) fwrite(wbuf, , S, stdout), wpos = ;
wbuf[wpos++] = x;
}
inline void wint(ll x)
{
if (x < ) wchar('-'), x = -x;
char s[];
int n = ;
while (x || !n) s[n++] = '' + x % , x /= ;
while (n--) wchar(s[n]);
wchar('\n');
}
inline void wstring(const char *s)
{
while (*s) wchar(*s++);
}
~FastIO()
{
if (wpos) fwrite(wbuf, , wpos, stdout), wpos = ;
}
} io;
int n,m,a[N][N],g[N],stk[N],L[N],R[N],top;
ll f[N];
int calc(int i,int j){return a[j][j]-a[i-][j]-a[j][i-]+a[i-][i-]>>;}
bool isbetter(int x,int y,int i){return f[x]+calc(x+,i)<f[y]+calc(y+,i)||f[x]+calc(x+,i)==f[y]+calc(y+,i)&&g[x]<g[y];}
pair<int,ll> check(int cost)
{
memset(f,,sizeof(f));f[]=;
stk[top=]=,L[]=,R[]=n;
for (int i=;i<=n;i++)
{
int l=,r=top,x;
while (l<=r)
{
int mid=l+r>>;
if (R[mid]>=i) x=stk[mid],r=mid-;
else l=mid+;
}
f[i]=f[x]+calc(x+,i)+cost;
g[i]=g[x]+;
while (L[top]>i&&isbetter(i,stk[top],L[top])) top--;
l=i+,r=R[top],x=R[top]+;
while (l<=r)
{
int mid=l+r>>;
if (isbetter(i,stk[top],mid)) x=mid,r=mid-;
else l=mid+;
}
R[top]=x-;if (x<=n) stk[++top]=i,L[top]=x,R[top]=n;
}
return make_pair(g[n],f[n]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj5311.in","r",stdin);
freopen("bzoj5311.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
a[i][j]=a[i-][j]+a[i][j-]+io.xint()-a[i-][j-];
int l=-,r=-l,ans;
while (l<=r)
{
int mid=l+r>>;
if (check(mid).first<=m) ans=mid,r=mid-;
else l=mid+;
}
cout<<check(ans).second-1ll*m*ans;
return ;
}
BZOJ5311 贞鱼(动态规划+wqs二分+决策单调性)的更多相关文章
- 【wqs二分 || 决策单调性】cf321E. Ciel and Gondolas
把状态看成层,每层决策单调性处理 题目描述 题目大意 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半.这不?他们遇到了大麻烦!n只贞鱼到陆地上乘车,现在有k辆汽车可以租用.由于贞鱼们 ...
- 【wqs二分 决策单调性】HHHOJ#261. Brew
第一道决策单调性…… 题目描述 HHHOJ#261. Brew 题目分析 挺好的……模板题? 寄存了先. #include<bits/stdc++.h> typedef long long ...
- BZOJ5252 八省联考2018林克卡特树(动态规划+wqs二分)
假设已经linkcut完了树,答案显然是树的直径.那么考虑这条直径在原树中是怎样的.容易想到其是由原树中恰好k+1条点不相交的链(包括单个点)拼接而成的.因为这样的链显然可以通过linkcut拼接起来 ...
- [CF321E]Ciel and Gondolas&&[BZOJ5311]贞鱼
codeforces bzoj description 有\(n\)个人要坐\(k\)辆车.如果第\(i\)个人和第\(j\)个人同坐一辆车,就会产生\(w_{i,j}\)的代价. 求最小化代价.\( ...
- bzoj5311: 贞鱼
还是年轻啊算的时候少乘一个4000被卡二分上界了...%%%%bright教我超级快速读D飞bzoj垃圾卡常数据 我们容易写出这样的DP方程:f[i][j]=f[k][j-1]+val(k+1,j) ...
- P3515-[POI2011]Lightning Conductor【整体二分,决策单调性】
正题 题目链接:https://www.luogu.com.cn/problem/P3507 题目大意 \(n\)个数字的一个序列\(a\),对于每个位置\(i\)求一个\(p_i\)使得对于任意\( ...
- DP的各种优化(动态规划,决策单调性,斜率优化,带权二分,单调栈,单调队列)
前缀和优化 当DP过程中需要反复从一个求和式转移的话,可以先把它预处理一下.运算一般都要满足可减性. 比较naive就不展开了. 题目 [Todo]洛谷P2513 [HAOI2009]逆序对数列 [D ...
- WQS二分题集
WQS二分,一种优化一类特殊DP的方法. 很多最优化问题都是形如“一堆物品,取与不取之间有限制.现在规定只取k个,最大/小化总收益”. 这类问题最自然的想法是:设f[i][j]表示前i个取j个的最大收 ...
- 【BZOJ5311/CF321E】贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性)
[BZOJ5311/CF321E]贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性) 题面 BZOJ CF 洛谷 辣鸡BZOJ卡常数!!!!!! 辣鸡BZOJ卡常数!!!!!! ...
随机推荐
- iRedMail退信问题的解决(转)
安装完iRedMail之后发现可以给外网发邮件但是收不到外网发来的邮件,查看log发现这么一句话:postfix/postscreen[11355]: NOQUEUE: reject: RCPT fr ...
- 字符串阵列String[]转换为整型阵列Int[]
原始数据: string input = "3,7,2,8,1,9,1,34,67,78,22"; 要处理为: " }; 最终处理为: , , , , , , , , , ...
- 一头雾水的"Follow The Pointer"
原文:一头雾水的"Follow The Pointer" 一头雾水的"Follow The Pointer" ...
- Sql 截取字段中的字符串
取 a 字段里有字符x后面的数 right(a, charindex('x',reverse(a))-1)) reverse(字段) 这个函数是把字段倒过来并转换成nvarchar类型 取 ...
- dotnetcore/CAP
CAP带你轻松玩转Asp.Net Core消息队列 CAP是什么? CAP是由我们园子里的杨晓东大神开发出来的一套分布式事务的决绝方案,是.Net Core Community中的第一个千星项目(目前 ...
- Error【0006】:could not create or update nagios.configtest
1. 错误背景 在本系列博客<Nagios监控系统部署(源码).md>中(笔记内链:Nagios监控系统部署(源码).md,博客园地址:https://www.cnblogs.com/li ...
- BugkuCTF 计算器
前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...
- Java多线程的使用以及原理
Java有两种方式实现多线程. 第一种——继承Thread类,并重写run方法 步骤: 定义类继承Thread类: 重写子类的run方法,将线程需要执行的代码写在run方法中: 创建子类的对象,则创建 ...
- 个人作业——final
一 . 对M1M2的一个总结 我特别感谢我们组的PM.以前我觉得女生学计算机这个专业,跟男生比差太远了.总觉得我们女生就是上上课写写作业考考试还行,但是一到开发什么项目啊,实战之类的,总觉得自己的能力 ...
- linux内实践核分析模块