题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1597
就是斜率优化水题…
然而WA了十几遍,正负号处理真让人心累…
还是该负就负,别乱换了…
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=;
int n,cnt,q[maxn],h,t;
ll f[maxn],x[maxn],y[maxn];
struct N{ll a,b;}p[maxn];
ll rd()
{
ll ret=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return ret*f;
}
//bool cmp(N x,N y){return x.a==y.a?x.b>y.b:x.a<y.a;}
bool cmp(N x,N y){return x.a==y.a?x.b>y.b:x.a<y.a;}
double slp(int a,int b){return (double)(f[b]-f[a])/(double)(y[a+]-y[b+]);}
int main()
{
n=rd();
for(int i=;i<=n;i++)p[i].a=rd(),p[i].b=rd();
sort(p+,p+n+,cmp);
for(int i=;i<=n;i++)
{
// if(p[i].a==p[i-1].a)continue;
// x[++cnt]=p[i].a; y[cnt]=p[i].b;
while (cnt && p[i].b>=y[cnt]) cnt--;
x[++cnt]=p[i].a; y[cnt]=p[i].b;
}
// h=1;t=1;//
for(int i=;i<=cnt;i++)
{
while(h<t&&slp(q[h],q[h+])<x[i])h++;//<
f[i]=f[q[h]]+x[i]*y[q[h]+];
while(h<t&&slp(q[t-],q[t])>slp(q[t],i))t--;
q[++t]=i;
}
printf("%lld",f[cnt]);
return ;
}