hdu 5128 The E-pang Palace

E-pang Palace was built in Qin dynasty by Emperor Qin Shihuang in Xianyang, Shanxi Province. It was the largest palace ever built by human. It was so large and so magnificent that after many years of construction, it still was not completed. Building the great wall, E-pang Palace and Qin Shihuang's tomb cost so much labor and human lives that people rose to fight against Qin Shihuang's regime. 

Xiang Yu and Liu Bang were two rebel leaders at that time. Liu Bang captured Xianyang -- the capital of Qin. Xiang Yu was very angry about this, and he commanded his army to march to Xianyang. Xiang Yu was the bravest and the strongest warrior at that time, and his army was much more than Liu Bang's. So Liu Bang was frighten and retreated from Xianyang, leaving all treasures in the grand E-pang Palace untouched. When Xiang Yu took Xianyang, he burned E-pang Palce. The fire lasted for more than three months, renouncing the end of Qin dynasty. 

Several years later, Liu Bang defeated Xiangyu and became the first emperor of Han dynasty. He went back to E-pang Palace but saw only some pillars left. Zhang Liang and Xiao He were Liu Bang's two most important ministers, so Liu Bang wanted to give them some awards. Liu Bang told them: "You guys can make two rectangular fences in E-pang Palace, then the land inside the fences will belongs to you. But the corners of the rectangles must be the pillars left on the ground, and two fences can't cross or touch each other." 

To simplify the problem, E-pang Palace can be consider as a plane, and pillars can be considered as points on the plane. The fences you make are rectangles, and you MUST make two rectangles. Please note that the rectangles you make must be parallel to the coordinate axes. 

The figures below shows 3 situations which are not qualified(Thick dots stands for pillars): 


Zhang Liang and Xiao He wanted the total area of their land in E-pang Palace to be maximum. Please bring your computer and go back to Han dynasty to help them so that you may change the history.

InputThere are no more than 15 test case. 

For each test case: 

The first line is an integer N, meaning that there are N pillars left in E-pang Palace(4 <=N <= 30). 

Then N lines follow. Each line contains two integers x and y (0 <= x,y <= 200), indicating a pillar's coordinate. No two pillars has the same coordinate. 

The input ends by N = 0.OutputFor each test case, print the maximum total area of land Zhang Liang and Xiao He could get. If it was impossible for them to build two qualified fences, print "imp".Sample Inputnode

8
0 0
1 0
0 1
1 1
0 2
1 2
0 3
1 3
8
0 0
2 0
0 2
2 2
1 2
3 2
1 3
3 3
0

Sample Outputios

2
imp


題目大意:給你一些點,從這些點中選擇八個點組成兩個矩形,求矩形面積的最大和是多少?兩個矩形不能以下圖形式:

 

 

解題思路:首先使用結構體存儲矩形的點,而後對於這些點有兩種排序方式。方式1:對x進行由小到大的排序(既上下排序);方式2:對y進行由小到大的排序(既左右排序),而後對於每一個找到的矩形,下一個知足條件的矩形要麼在它的右邊;要麼在他的上邊。注意:若是字形的矩形咱們只要求出大矩形的面積和大矩形以外的矩形的面積只和就能夠了。c++

 
 
AC代碼:
  1 #include <iostream>
  2 #include <bits/stdc++.h>
  3 
  4 using namespace std;
  5 struct node
  6 {
  7     int x,y;
  8 } a[50];
  9 int flag[2000][2000];
 10 int cmp2(const node &p,const node &q)
 11 {
 12     if(p.x!=q.x)
 13         return p.x<q.x;
 14     else
 15         return p.y<q.y;
 16 }
 17 int cmp1(const node &p,const node &q)
 18 {
 19     if(p.y!=q.y)
 20         return p.y<q.y;
 21     else
 22         return p.x<q.x;
 23 }
 24 int main()
 25 {
 26     int n;
 27     while(~scanf("%d",&n))
 28     {
 29         if(n==0)
 30         break;
 31         memset(flag,0,sizeof(flag));
 32         for(int i=0; i<n; i++)
 33         {
 34             scanf("%d%d",&a[i].x,&a[i].y);
 35             flag[1000+a[i].x][1000+a[i].y]=1;
 36         }
 37         int maxx=0,cnt=0;
 38         sort(a,a+n,cmp1);
 39         for(int i=0; i<n; i++)
 40         {
 41             for(int j=i+1; j<n; j++)
 42             {
 43                 if(a[j].x<=a[i].x||a[j].y<=a[i].y)
 44                     continue;
 45                 if(flag[a[i].x+1000][a[j].y+1000]==1&&flag[a[j].x+1000][a[i].y+1000]==1)
 46                 {
 47                     for(int u=i+1;u<j;u++)
 48                     {
 49                         for(int v=u+1;v<j;v++)
 50                         {
 51                             if((a[u].x>a[i].x&&a[v].x>a[u].x&&a[j].x>a[v].x)&&(a[u].y>a[i].y&&a[v].y>a[u].y&&a[j].y>a[v].y)&&(flag[a[u].x+1000][a[v].y+1000]==1&&flag[a[v].x+1000][a[u].y+1000]==1))
 52                             {
 53                                cnt=1;
 54                                maxx=max(maxx,(a[j].x-a[i].x)*(a[j].y-a[i].y));
 55                             }
 56                         }
 57                     }
 58                     int k;
 59                     for(k=0; k<n; k++)
 60                         if(a[k].y>a[j].y)
 61                             break;
 62                     for(int u=k; u<n; u++)
 63                     {
 64                         for(int v=u+1; v<n; v++)
 65                         {
 66                             if(a[v].x<=a[u].x||a[v].y<=a[u].y)
 67                                 continue;
 68                             if(flag[a[u].x+1000][a[v].y+1000]==1&&flag[a[v].x+1000][a[u].y+1000]==1)
 69                             {
 70                                 cnt=1;
 71                                 maxx=max(maxx,(a[j].x-a[i].x)*(a[j].y-a[i].y)+(a[u].x-a[v].x)*(a[u].y-a[v].y));
 72                             }
 73                         }
 74                     }
 75                     node eps[2];
 76                     eps[0].x=a[i].x;
 77                     eps[0].y=a[i].y;
 78                     eps[1].x=a[j].x;
 79                     eps[1].y=a[j].y;
 80                     sort(a,a+n,cmp2);
 81                     for(k=0; k<n; k++)
 82                         if(a[k].x>eps[1].x)
 83                             break;
 84                     for(int u=k; u<n; u++)
 85                     {
 86                         for(int v=k; v<n; v++)
 87                         {
 88                             if(a[v].x<=a[u].x||a[v].y<=a[u].y)
 89                                 continue;
 90                             if(flag[a[u].x+1000][a[v].y+1000]==1&&flag[a[v].x+1000][a[u].y+1000]==1)
 91                             {
 92                                 cnt=1;
 93                                 maxx=max(maxx,(eps[1].x-eps[0].x)*(eps[1].y-eps[0].y)+(a[u].x-a[v].x)*(a[u].y-a[v].y));
 94                             }
 95                         }
 96                     }
 97                 }
 98             }
 99             sort(a,a+n,cmp1);
100         }
101         if(cnt==0)
102         {
103             printf("imp\n");
104         }
105         else
106         {
107             printf("%d\n",maxx);
108         }
109     }
110     return 0;
111 }
View Code
相關文章
相關標籤/搜索