STL 双向队列DEQUE版本

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 50010
#define LL long long struct node
{
LL x,y;
}t[Maxn]; bool cmp(node x,node y) {return (x.x==y.x)?(x.y<y.y):(x.x<y.x);} deque<node > q;
LL f[Maxn]; bool check(node x,node y,LL k)
{
return (y.y-x.y)<=k*(y.x-x.x);
} bool check2(node x,node y,node z)
{
return (y.y-x.y)*(z.x-y.x)>=(y.x-x.x)*(z.y-y.y);
} int main()
{
LL n;
scanf("%lld",&n);
for(LL i=;i<=n;i++) scanf("%lld%lld",&t[i].x,&t[i].y);
sort(t+,t++n,cmp);
/*LL p=1;
for(LL i=2;i<=n;i++) if(t[i].x>t[p].x&&t[i].y<t[p].y) t[++p]=t[i];*/ LL p=;
for(LL i=;i<=n;i++)
{
while(p>&&t[i].y>=t[p].y) p--;
t[++p]=t[i];
} while(!q.empty()) q.pop_back();
node ft;ft.x=t[].y,ft.y=;q.push_front(ft);
for(LL i=;i<=p;i++)
{
while(q.size()>)
{
node x=q.front();q.pop_front();
if(!check(x,q.front(),-t[i].x)) {q.push_front(x);break;}
}
node tt;
f[i]=q.front().y+t[i].x*q.front().x;
tt.x=t[i+].y;tt.y=f[i];
while(q.size()>)
{
node x=q.back();q.pop_back();
if(!check2(tt,x,q.back())) {q.push_back(x);break;}
}
q.push_back(tt);
// printf("%d\n",f[i]);
}
printf("%lld\n",f[p]);
return ;
}

手打队列版本

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 50010
#define LL long long struct hp
{
LL x,y;
}a[Maxn]; bool cmp(hp x,hp y) {return (x.x==y.x)?(x.y<y.y):(x.x<y.x);} struct node
{
LL x,y;
}t[Maxn]; LL f[Maxn]; bool check(int x,int y,int k)
{
LL kk=k;
return kk*(t[x].x-t[y].x)<=t[x].y-t[y].y;
} bool check2(int x,int y,int z)
{
return (t[y].x-t[z].x)*(t[x].y-t[y].y)<=(t[x].x-t[y].x)*(t[y].y-t[z].y);
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
sort(a+,a++n,cmp);
int cnt=;
for(int i=;i<=n;i++)
{
while(cnt>&&a[i].y>=a[cnt].y) cnt--;
a[++cnt]=a[i];
}
int len=,st;
t[++len].x=a[].y;t[len].y=;st=;
for(int i=;i<=cnt;i++)
{
while(st<len&&check(st,st+,-a[i].x)) st++;
f[i]=a[i].x*t[st].x+t[st].y;
t[].x=a[i+].y;t[].y=f[i];
while(st<len&&check2(len-,len,)) len--;
t[++len]=t[];
// printf("%lld\n",f[i]);
}
printf("%lld\n",f[cnt]);
return ;
}

斜率优化

2016-11-18 08:30:47

【无聊放个模板系列】BZOJ 1597 斜率优化的更多相关文章

  1. 【无聊放个模板系列】BZOJ 3172 (AC自动机)

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  2. 【无聊放个模板系列】HDU 3506 (四边形不等式优化DP-经典石子合并问题[环形])

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  3. 【无聊放个模板系列】POJ 3678 2-SAT

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  4. 【无聊放个模板系列】POJ 1274 (匈牙利)

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  5. 【无聊放个模板系列】HDU 1269 (SCC)

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  6. 【无聊放个模板系列】HDU 1358 KMP

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  7. 【无聊放个模板系列】HDU 3068 MANACHER

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  8. 【无聊放个模板系列】POJ2752 EXKMP

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  9. 学渣乱搞系列之dp斜率优化

    学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很 ...

随机推荐

  1. C#中Excel的导入和导出的几种基本方式

    在上一篇(http://www.cnblogs.com/fengchengjushi/p/3369386.html)介绍过,Excel也是数据持久化的一种实现方式.在C#中.我们常常会与Excel文件 ...

  2. 验证hashmap非线程安全

    http://www.blogjava.net/lukangping/articles/331089.html final HashMap<String, String> firstHas ...

  3. 原生JS面向对象思想封装轮播图组件

    原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...

  4. SpringMVC中注解和非注解方式下的映射器和适配器总结

    1. 非注解方式 1.1 处理器适配器 上一节中使用的处理器适配器是:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapte ...

  5. PerformSelector may cause a leak because its selector is unknown 解决方法

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3801030.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  6. Android - 代码片段

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-code-snippets/ 说明 此篇文章为个人日常使用所整理的一此代码片段,此篇文正将会不 ...

  7. linux命令之ps命令

    1.管道 linux命令管道通过|表示.一般在linux命令中|(管道)之前的命令会输出大量的结果,|(管道)之后的命令一般就是带有条件的,只将|前满足条件的结果显示出来. 2.grep命令 grep ...

  8. linux centos 安装

    本着学习的目的,在自己的电脑上进行 centos 7 安装,记录下这步骤以备忘. 一.Centos 下载 centos 官方(https://www.centos.org/)下载ISO镜像(这是我的下 ...

  9. pdf转chm的实现方法

    相比pdf, CHM电子书在Windows系统下不需要安装额外的浏览器即可进行阅读,其内容是基于浏览器的风格,更容易被用户所接受.而且, 具有更强大的功能配置,比如可提供强大的全文搜索.索引.书签等的 ...

  10. ECshop sina

    http://blog.sina.com.cn/s/blog_5327408801019ofa.html http://blog.sina.com.cn/u/2624360105 http://blo ...