CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)
这题就是斜率DP(555...早知道直接做第3题)
方程我就不写了
long long 溢出我居然De了一上午Bug才发现,我……
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #include<cmath> #include<cctype> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define ForD(i,n) for(int i=n;i;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define RepD(i,n) for(int i=n;i>=0;i--) #define MEM(a) memset(a,0,sizeof(a)) #define MEMI(a) memset(a,127,sizeof(a)) #define MEMi(a) memset(a,128,sizeof(a)) #define MAXN (100000+10) #define size (tail-head+1) int n; long long a[MAXN],b[MAXN],f[MAXN]={0}; struct node { int i; long long x,y; node(){} node(int _i):i(_i),x(b[i]),y(f[i]){} friend long double k(node a,node b){return (long double)(a.y-b.y)/(long double)(a.x-b.x); } }q[MAXN]; int head=1,tail=1; struct V { long long x,y; V(node a,node b):x(b.x-a.x),y(b.y-a.y){} friend long long operator*(V a,V b){return ((double)a.x/a.y-(double)b.x/b.y)>0?1:-1; } }; int main() { // freopen("CF319C.in","r",stdin); // freopen("CF319C.out","w",stdout); cin>>n; For(i,n) cin>>a[i]; For(i,n) cin>>b[i]; f[1]=b[1]; q[1]=node(1); Fork(i,2,n) { while (size>=2&&k(q[head],q[head+1])>1-a[i] ) head++; int j=q[head].i; f[i]=f[j]+(long long)(a[i]-1)*(long long)b[j]+b[i]; while (size>=2&&(V(q[tail-1],q[tail])*V(q[tail],node(i))>0)) tail--; //维护上凸壳 q[++tail]=node(i); } //For(i,n) cout<<b[i]<<' ';cout<<endl; //For(i,n) cout<<f[i]<<' ';cout<<endl; cout<<f[n]<<endl; return 0; }