sdut 2401 最大矩形面积

      1http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2401

/* 2 最大矩形面积,把边界点加上 3 从左往右 处理一遍; 4 再从上往下处理一遍; 5 */ 6 7 #include<stdio.h> 8 #define maxn 20000 9 #include<cmath> 10 #include<algorithm> 11 using namespace std; 12 int min(int x,int y) 13 { 14 if(x<y)return x; 15 else return y; 16 } 17 int max(int x,int y) 18 { 19 if(x>y)return x; 20 else return y; 21 } 22 struct node 23 { 24 int x; 25 int y; 26 }p[maxn]; 27 int n,l,w; 28 int cmpx(node a,node b) 29 { 30 if(a.x!=b.x)return a.x<b.x; 31 else return a.y<b.y; 32 } 33 int cmpy(node a,node b) 34 { 35 if(a.y!=b.y)return a.y<b.y; 36 else return a.x<b.x; 37 } 38 int LR() 39 { 40 int i,j; 41 int top,down; 42 int ans=-1; 43 for(i=0;i<n-1;i++) 44 { 45 top=w;down=0; 46 for(j=i+1;j<n;j++) 47 { 48 if(p[i].x!=p[j].x) 49 ans=max(ans,fabs(p[i].x-p[j].x)*(top-down)); 50 51 if(p[j].y>p[i].y)top=min(top,p[j].y); 52 else 53 down=max(down,p[j].y); 54 } 55 } 56 return ans; 57 } 58 int UD() 59 { 60 int i,j; 61 int L,R; 62 int ans=-1; 63 for(i=0;i<n-1;i++) 64 { 65 L=0;R=l; 66 for(j=i+1;j<n;j++) 67 { 68 if(p[i].y!=p[j].y) 69 ans=max(ans,fabs(p[i].y-p[j].y)*(R-L)); 70 71 if(p[j].x>p[i].x)R=min(R,p[j].x); 72 else 73 L=max(L,p[j].x); 74 } 75 } 76 return ans; 77 } 78 int main() 79 { 80 int T,i; 81 scanf("%d",&T); 82 while(T--) 83 { 84 scanf("%d%d",&l,&w); 85 scanf("%d",&n); 86 if(n==0) 87 { 88 printf("%d\n",l*w); 89 continue; 90 } 91 p[0].x=0;//加上边界点 92 p[0].y=0; 93 p[1].x=l; 94 p[1].y=w; 95 n=n+2; 96 for(i=2;i<n;i++) 97 { 98 scanf("%d%d",&p[i].x,&p[i].y); 99 } 100 sort(p,p+n,cmpx); 101 int s1=LR(); 102 sort(p,p+n,cmpy); 103 int s2=UD(); 104 printf("%d\n",max(s1,s2)); 105 } 106 }
原文地址:https://www.cnblogs.com/acSzz/p/2485845.html