6.13校内互测 (DP 带权二分 斜率优化)
丘中有麻plant
改自这儿,by ZBQ。





还有隐藏的一页不放了。。
直接走下去的话,如果开始时间确定那么到每个点的时间确定,把time减去dis就可以去掉路程的影响了。
这样对于减去d后的t,如果想要摘一部分,那么应是取其中最大的t恰好摘它,其它t较小的会早熟然后等着。。(意会一下吧)
所以t大的会对t小的产生贡献,而要恰好摘t小的,那就摘不了t大的了。
所以对t排序并不会影响答案。从小到大依次分K段就行了。i对其中每个作物j的贡献是ti-tj。
注意t相等时虽然会同时摘,但是不能直接去重!因为如果不恰好摘它们,其它的会对它们所有产生贡献。。
所以考试的时候只有10分。。
某些剪枝之类的优化还是想好再加吧。没啥用又不会被卡T,不如不加。
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 200000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=2e5+5;
int n,K,q[N],num[N];//longlong
LL tm[N],sum[N],f[N],C;
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
namespace Spec
{
int n,Now,q[N];//longlong
LL sum[N],f[2][N];
inline LL Y(int j,int k){
return f[Now][j]-f[Now][k]+sum[j]-sum[k];
}
inline LL X(int j,int k){
return j-k;
}
inline LL Calc(int fr,int to,int k){
return f[Now][fr]+(to-fr)*tm[to]-sum[to]+sum[fr];
}
void Main()
{
for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+(LL)tm[i];
memset(f,0x7f,sizeof f);
f[0][0]=0, Now=0;
for(int j=1; j<=K; ++j, Now^=1)
{
int h=1,t=1; q[1]=j-1;// q[1]=0;
for(int i=j; i<=n; ++i)
{
while(h<t && Y(q[h+1],q[h])<=1ll*tm[i]*X(q[h+1],q[h])) ++h;
f[Now^1][i]=Calc(q[h],i,j);
while(h<t && Y(i,q[t])*X(q[t],q[t-1])<=Y(q[t],q[t-1])*X(i,q[t])) --t;
q[++t]=i;
}
}
printf("%I64d\n",f[Now][n]);
}
}
inline LL Y(int j,int k){
return f[j]-f[k]+sum[j]-sum[k];
}
inline LL X(int j,int k){
return j-k;
}
inline LL Calc(int fr,int to){
return f[fr]+tm[to]*(to-fr)-sum[to]+sum[fr]+C;
}
void Solve()
{
f[0]=num[0]=0;
int h=1,t=1; q[1]=0;
for(int i=1; i<=n; ++i)
{
while(h<t && Y(q[h+1],q[h])<=tm[i]*X(q[h+1],q[h])) ++h;
f[i]=Calc(q[h],i), num[i]=num[q[h]]+1;
while(h<t && Y(i,q[t])*X(q[t],q[t-1])<=Y(q[t],q[t-1])*X(i,q[t])) --t;
q[++t]=i;
}
}
int main()
{
freopen("plant.in","r",stdin);
freopen("plant.out","w",stdout);
n=read(), K=read();
int s=0;
tm[1]=read(), read();
for(int i=2; i<=n; ++i) tm[i]=read()-(s+=read());
std::sort(tm+1,tm+1+n);
// int cnt=n; n=1;// WA!
// for(int i=2; i<=cnt; ++i) if(tm[i]!=tm[i-1]) tm[++n]=tm[i];
for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+tm[i];
if(1ll*n*K<=2e7) {Spec::n=n, Spec::Main(); return 0;}
// Spec::n=n, Spec::Main(); return 0;
LL l=sum[n]*n, r=-l;
if(l>r) std::swap(l,r);
while(l<=r){
if(C=l+r>>1, Solve(), num[n]>K) l=C+1;
else r=C-1;
}
C=l, Solve();
// C=r+1, Solve();
printf("%I64d\n",f[n]-C*K);
return 0;
}
这是\(O(n^2k)\)暴力和\(O(nk)\)斜率优化,还有个改double的带权二分(开始拍出错tm[]爆int了),都去重了所以对拍虽然过了然而。。
就这题会所以特别不嫌麻烦。
暴力:
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
//#define int long long
typedef long long LL;
const int N=1e4+5;
int n,K,tm[N];
LL sum[N],f[N][250];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline LL Calc(int fr,int to,int k){
return f[fr][k-1]+1ll*(to-fr)*tm[to]-sum[to]+sum[fr];
}
int main()
{
freopen("plant.in","r",stdin);
// freopen("plant.out","w",stdout);
freopen("violence.out","w",stdout);
n=read(), K=read();
int s=0;
tm[1]=read(), read();
for(int i=2; i<=n; ++i) tm[i]=read()-(s+=read());
// for(int i=1; i<=n; ++i) printf("%d:%d\n",i,tm[i]);
std::sort(tm+1,tm+1+n);
int cnt=n; n=1;
for(int i=2; i<=cnt; ++i) if(tm[i]!=tm[i-1]) tm[++n]=tm[i];
// for(int i=1; i<=n; ++i) printf("%d:%d\n",i,tm[i]);putchar('\n');
for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+(LL)tm[i];
memset(f,0x7f,sizeof f);
f[0][0]=0;
for(int i=1; i<=n; ++i)
for(int j=1; j<=K; ++j)
for(int k=0; k<i; ++k)
f[i][j]=std::min(f[i][j],Calc(k,i,j));
printf("%I64d\n",f[n][K]);
return 0;
}
裸斜率优化
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=2e5+5;
int n,K,Now,q[N];//longlong
LL tm[N],sum[N],f[2][N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline LL Y(int j,int k){
return f[Now][j]-f[Now][k]+sum[j]-sum[k];
}
inline LL X(int j,int k){
return j-k;
}
inline LL Calc(int fr,int to,int k){
return f[Now][fr]+tm[to]*(to-fr)-sum[to]+sum[fr];
}
int main()
{
freopen("plant.in","r",stdin);
// freopen("plant.out","w",stdout);
freopen("slope.out","w",stdout);
n=read(), K=read();
int s=0;
tm[1]=read(), read();
for(int i=2; i<=n; ++i) tm[i]=read()-(s+=read());
std::sort(tm+1,tm+1+n);
int cnt=n; n=1;
for(int i=2; i<=cnt; ++i) if(tm[i]!=tm[i-1]) tm[++n]=tm[i];
for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+tm[i];
memset(f,0x7f,sizeof f);
f[0][0]=0, Now=0;
for(int j=1; j<=K; ++j, Now^=1)
{
int h=1,t=1; q[1]=j-1;// q[1]=0;
for(int i=j; i<=n; ++i)
{
while(h<t && Y(q[h+1],q[h])<=tm[i]*X(q[h+1],q[h])) ++h;
f[Now^1][i]=Calc(q[h],i,j);
while(h<t && Y(i,q[t])*X(q[t],q[t-1])<=Y(q[t],q[t-1])*X(i,q[t])) --t;
q[++t]=i;
}
}
printf("%I64d\n",f[Now][n]);
return 0;
}
double带权二分。。:
//also right.
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
//#define gc() getchar()
#define eps (1e-2)
#define MAXIN 200000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=2e5+5;
int n,K,tm[N],q[N],num[N];//longlong
LL sum[N];
double f[N],C;
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline double Y(int j,int k){
return f[j]-f[k]+sum[j]-sum[k];
}
inline double X(int j,int k){
return j-k;
}
inline double Calc(int fr,int to){
return f[fr]+1.0*(to-fr)*tm[to]-(double)sum[to]+(double)sum[fr]+C;
}
void Solve()
{
f[0]=num[0]=0;
int h=1,t=1; q[1]=0;
for(int i=1; i<=n; ++i)
{
while(h<t && Y(q[h+1],q[h])<=1.0*tm[i]*X(q[h+1],q[h])) ++h;
f[i]=Calc(q[h],i), num[i]=num[q[h]]+1;
while(h<t && Y(i,q[t])*X(q[t],q[t-1])<=Y(q[t],q[t-1])*X(i,q[t])) --t;
q[++t]=i;
}
}
int main()
{
freopen("plant.in","r",stdin);
// freopen("tmp.out","w",stdout);
n=read(), K=read();
int s=0;
tm[1]=read(), read();
for(int i=2; i<=n; ++i) tm[i]=read()-(s+=read());
std::sort(tm+1,tm+1+n);
int cnt=n; n=1;
for(int i=2; i<=cnt; ++i) if(tm[i]!=tm[i-1]) tm[++n]=tm[i];
for(int i=1; i<=n; ++i) sum[i]=sum[i-1]+(LL)tm[i];
double l=1.0*sum[n]*n, r=-l;
if(l>r) std::swap(l,r);
while(r>l+eps){
if(C=(l+r)*0.5, Solve(), num[n]>K) l=C+1;
else r=C-1;
}
C=l, Solve();
// C=r+1, Solve();
printf("%I64d\n",(LL)(f[n]-C*K+0.5));
return 0;
}
6.13校内互测 (DP 带权二分 斜率优化)的更多相关文章
- 洛谷 4383 [八省联考2018]林克卡特树lct——树形DP+带权二分
题目:https://www.luogu.org/problemnew/show/P4383 关于带权二分:https://www.cnblogs.com/flashhu/p/9480669.html ...
- 洛谷.4383.[八省联考2018]林克卡特树lct(树形DP 带权二分)
题目链接 \(Description\) 给定一棵边带权的树.求删掉K条边.再连上K条权为0的边后,新树的最大直径. \(n,K\leq3\times10^5\). \(Solution\) 题目可以 ...
- Codeforces.739E.Gosha is hunting(DP 带权二分)
题目链接 \(Description\) 有\(n\)只精灵,两种精灵球(高级和低级),每种球能捕捉到第\(i\)只精灵的概率已知.求用\(A\)个低级球和\(B\)个高级球能捕捉到精灵数的最大期望. ...
- DP的各种优化(动态规划,决策单调性,斜率优化,带权二分,单调栈,单调队列)
前缀和优化 当DP过程中需要反复从一个求和式转移的话,可以先把它预处理一下.运算一般都要满足可减性. 比较naive就不展开了. 题目 [Todo]洛谷P2513 [HAOI2009]逆序对数列 [D ...
- P4383 [八省联考2018]林克卡特树lct 树形DP+凸优化/带权二分
$ \color{#0066ff}{ 题目描述 }$ 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的 ...
- 洛谷P2619 [国家集训队2]Tree I(带权二分,Kruscal,归并排序)
洛谷题目传送门 给一个比较有逼格的名词--WQS二分/带权二分/DP凸优化(当然这题不是DP). 用来解决一种特定类型的问题: 有\(n\)个物品,选择每一个都会有相应的权值,需要求出强制选\(nee ...
- BZOJ_4609_[Wf2016]Branch Assignment_决策单调性+带权二分
BZOJ_4609_[Wf2016]Branch Assignment_决策单调性+带权二分 Description 要完成一个由s个子项目组成的项目,给b(b>=s)个部门分配,从而把b个部门 ...
- BZOJ_5311_贞鱼_决策单调性+带权二分
BZOJ_5311_贞鱼_决策单调性+带权二分 Description 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半. 这不?他们遇到了大麻烦! n只贞鱼到陆地上乘车,现在有k辆汽 ...
- HDU 4359 Easy Tree DP? 带权二叉树的构造方法 dp
题意: 给定n deep 1.构造一个n个节点的带权树,且最大深度为deep,每一个节点最多仅仅能有2个儿子 2.每一个节点的值为2^0, 2^1 ··· 2^(n-1) 随意两个节点值不能同样 3 ...
随机推荐
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- Strusts2笔记9--防止表单重复提交和注解开发
防止表单重复提交: 用户可能由于各种原因,对表单进行重复提交.Struts2中使用令牌机制防止表单自动提交.以下引用自北京动力节点:
- CentOS_5.5_安装GCC编译LiME
1 概述 近期遇到个使用CentOS 5.5的系统,生产环境没有GCC.GDB.要对这台机器抓取关键内存回去用volatility分析. 思路1:使用工具Dump某个进程的内存.使用cat /proc ...
- iptables NAT规则【转】
nat表需要的三个链: 1.PREROUTING:可以在这里定义进行目的NAT的规则,因为路由器进行路由时只检查数据包的目的ip地址,所以为了使数据包得以正确路由,我们必须在路由之前就进行目的NAT; ...
- CSS锚伪类顺序需注意的几点
CSS锚伪类有以下几种: a:link{color:pink} /*未访问的链接*/ a:visited{color:red} /*已访问的链接*/ a:hover{color:blue} /*鼠标移 ...
- 27 Debugging Go Code with GDB 使用GDB调试go代码
Debugging Go Code with GDB 使用GDB调试go代码 Introduction Common Operations Go Extensions Known Issues Tu ...
- 基于timestamp和nonce的防止重放攻击方案
参考:http://blog.csdn.net/koastal/article/details/53456696
- tomcat数据源配置DBCP
原文件: https://www.cnblogs.com/sicd/p/4053780.html DBCP object created 日期 by the following code was ne ...
- Codefroces 628B New Skateboard(数位+思维)
题目链接:http://codeforces.com/contest/628/problem/B 题目大意:给你一段数字串s(1?≤?|s|?≤?3·10^5),求该字符串有多少子串是4的倍数.解题思 ...
- CVE-2012-4792Microsoft Internet Explorer 释放后使用漏洞
Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏览器. Microsoft Internet Explorer 6至8版本中存在释放后使用漏洞.通 ...