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 Input

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 Output

2
imp

题目大意:给你一些点,从这些点中选择八个点组成两个矩形,求矩形面积的最大和是多少?两个矩形不能如下图形式:

解题思路:首先使用结构体存储矩形的点,然后对于这些点有两种排序方式。方式1:对x进行由小到大的排序(既上下排序);方式2:对y进行由小到大的排序(既左右排序),然后对于每个找到的矩形,下一个满足条件的矩形要么在它的右边;要么在他的上边。注意:如果字形的矩形我们只要求出大矩形的面积和大矩形之外的矩形的面积只和就可以了。

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 }

				
时间: 2024-10-05 23:37:29

hdu 5128 The E-pang Palace的相关文章

HDU 5128 The E-pang Palace(2014广州赛区现场赛B题 计算几何)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 解题报告:在一个平面上给出n个点的坐标,用这n个点作为矩形的四个顶点,作两个矩形,要求两个矩形不能相交,也不能有边和点相交,然后两个矩形的面积之和要最大,求最大的面积之和是多少?如果不存在输出imp 因为n <=30,所以可以先把所有的矩形枚举出来,然后再暴力判断两两矩形组合,首先要满足位置关系,然后取面积和最大就是了.要注意的地方就是要小心一个大矩形包含一个小矩形的情况,在这种情况下,是满足

hdu - 5128 The E-pang Palace(枚举+计算几何)

http://acm.hdu.edu.cn/showproblem.php?pid=5128 给出n个点,求n个点组成两个矩形的最大面积. 矩形必须平行x轴,并且不能相交,但是小矩形在大矩形内部是可以的,面积就为大矩形的面积. 我是枚举一条对角线,然后去找另外两个点是否在坐标中存在这样就可以确定一个矩形, 同理可以枚举出另外有一个矩形,但是要注意坐标不能重复, 判断矩形相交的话,只要判断一个矩形的4个点是否有在另一个矩形的范围内即可. 1 #include<cstdio> 2 #include

HDU 5128 The E-pang Palace(暴力)

The E-pang Palace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 251    Accepted Submission(s): 93 Problem Description E-pang Palace was built in Qin dynasty by Emperor Qin Shihuang in Xiany

HDU 5128 The E-pang Palace(暴力瞎搞)

题目大意:给你n个点,让你组成矩形,然后如果有两个矩形不相交的话就计算他们的面积,求最大的两个矩形的面积并.注意的是回字型的嵌套,面积的并是最大的矩形的面积. 解题思路:暴力,枚举出来矩形,然后再暴力枚举两个矩形判断是否相交,是否为回字型. The E-pang Palace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 288 

hdu 5129 (枚举) The E-pang Palace

题目;http://acm.hdu.edu.cn/showproblem.php?pid=5128. 给你n个点,问能否组成两个不相交的与坐标轴平行的矩形,能就输出两矩形的面积和,不能就输出一个字符串. 由于n的范围就30,所以就是枚举一下就行,先将能够组成的矩形找出来,然后再两两比较,注意的是有一个矩形包含另一个矩形的 情况,此时和就是大矩形的面积.写的时候细心一点就好. 1 #include<cstdio> 2 #include<cstring> 3 #include<i

几何统计 ACM ICPC

2014 ACM ICPC 广州现场赛 HDU 5128 The E-pang Palace HDU 5134 Highway HDU 5135 Little Zu Chongzhi's Triangles 2014 ACM ICPC 北京现场赛 HDU 5120 Intersection 2014 ACM ICPC 上海网络赛 HDU 5048 (待做,三维几何)

HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题面: The E-pang Palace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 1646    Accepted Submission(s): 1226 Problem Description E-pang Palac

【HDU 5721】Palace(平面最近点对)

[HDU 5721]Palace(平面最近点对) Palace Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 404    Accepted Submission(s): 104 Problem Description The last trial Venus imposes on Psyche is a quest to the

HDU - 5128The E-pang Palace+暴力枚举,计算几何

第一次写计算几何,ac,感动. 不过感觉自己的代码还可以美化一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意: 在一个坐标系中,有n个点,从中找到两个互不touch,互不cross的两个矩形(边要和坐标轴平行),使得面积最大. 思路: 枚举每个点,n的四次方,这题有个坑点是“回型矩阵”,是真的坑,然后discuss区里说的十字形矩阵是骗人的. 每次对枚举的四个点进行check,如果两个矩阵的四个点相互不在对方的矩阵中,那么这组就是