bzoj 1597: [Usaco2008 Mar]土地购买
式子显然随便搞搞就行,,而且可以先把这些矩形排序,然后如果有比当前矩形x和y都大的矩形,这个矩形是可以忽略的。
1 #include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 0x3f3f3f3f 5 #define eps 1e-5 6 #define N 100005 7 using namespace std; 8 inline int ra() 9 { 10 int x=0,f=1; char ch=getchar(); 11 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 12 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 13 return x*f; 14 } 15 int n,tot; 16 LL x[N],y[N],f[N]; 17 int q[N]; 18 struct node{int x,y;}a[N]; 19 bool cmp(node a, node b){return a.x==b.x?a.y<b.y:a.x<b.x;} 20 double slope(int a, int b){return (f[b]-f[a])/(y[a+1]-y[b+1]);} 21 int main(int argc, char const *argv[]) 22 { 23 n=ra(); 24 for (int i=1; i<=n; i++) a[i].x=ra(),a[i].y=ra(); 25 sort(a+1,a+n+1,cmp); 26 for (int i=1; i<=n; i++) 27 { 28 while (tot && a[i].y>=y[tot]) tot--; 29 x[++tot]=a[i].x; y[tot]=a[i].y; 30 } 31 int l=0,r=0; 32 for (int i=1; i<=tot; i++) 33 { 34 while (l<r && slope(q[l],q[l+1])<x[i]) l++; 35 int t=q[l]; 36 f[i]=f[t]+y[t+1]*x[i]; 37 while (l<r && slope(q[r],i)<slope(q[r-1],q[r])) r--; 38 q[++r]=i; 39 } 40 printf("%lld\n",f[tot]); 41 return 0; 42 }