bzoj1597 [Usaco2008 Mar]土地购买
还是不太会斜率优化
果然我还是太弱了
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 struct xl 34 { 35 int x,y; 36 bool op < (const xl &a)const {return x==a.x?y<a.y:x<a.x;} 37 }; 38 xl a[50050]; 39 xl sta[50050]; 40 int que[50050]; 41 int top,n; 42 LL ans[50050]; 43 double k(int a,int b) 44 { 45 return 1.*(ans[b]-ans[a])/(1.*(sta[a+1].y-sta[b+1].y)); 46 } 47 int main() 48 { 49 inin(n); 50 re(i,1,n)inin(a[i].x),inin(a[i].y); 51 sort(a+1,a+n+1); 52 re(i,1,n) 53 { 54 while(top&&sta[top].y<=a[i].y)top--; 55 sta[++top]=a[i]; 56 } 57 int l=0,r=0; 58 re(i,1,top) 59 { 60 while(l<r&&k(que[l],que[l+1])<sta[i].x)l++; 61 ans[i]=ans[que[l]]+(LL)sta[que[l]+1].y*sta[i].x; 62 while(l<r&&k(que[r],i)<k(que[r-1],que[r]))r--; 63 que[++r]=i; 64 } 65 printf("%lld",ans[top]); 66 return 0; 67 }