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