1. 程式人生 > >最大矩形面積

最大矩形面積

Problem Description

在一個矩形區域內有很多點,每個點的座標都是整數。求一個矩形,使之內部沒有點,且面積最大。所求矩形的邊與座標軸平行。

Input

一個整數t,表示測試組數。 整數l,w表示矩形橫向邊長和豎向邊長。  一個整數n,表示該矩形內點的個數。  n個點的座標x,y。

Output

最大面積。

Sample Input

2
2 3
0
10 10
4
1 1
9 1
1 9
9 9 

Sample Output

6
80

Hint

Source

程式碼:

import java.util.Scanner; class Point{     int l;     int r;     public Point(int l,int r)     {         this.l = l;         this.r = r;     } } public class Main {     public static Point p[] = new Point[10000];     public static void main(String[] args) {         Scanner reader = new Scanner(System.in);         int lmax,rmax;         int T = reader.nextInt();         while(T-->0)         {             int l = reader.nextInt();///chang             int w = reader.nextInt();///kuan             int n = reader.nextInt();                          for(int i=0;i<n;i++)             {                 int x = reader.nextInt();                 int y = reader.nextInt();                 p[i] = new Point(x,y);             }             p[n] = new Point(0,0);///新加的點             p[n+1] = new Point(l,w);             lmax = getl(n,w);             rmax = getr(n,l);             System.out.println(Math.max(lmax,rmax));         }         reader.close();     }     public static int getl(int n,int w)     {         /*          * 根據橫座標的大小排序。          */         Point t = new Point(0,0);         for(int i=0;i<n+2;i++)///多加了兩個點         {             for(int j=i+1;j<n+2;j++)             {                 if(p[i].l>p[j].l)                 {                     t = p[i];                     p[i] = p[j];                     p[j] = t;                 }                 else if(p[i].l==p[j].l&&p[i].r>p[j].r)                 {                     t = p[i];                     p[i] = p[j];                     p[j] = t;                 }             }         }         int sum = 0;         int up = 0;         int down = 0;         for(int i=0;i<n+2;i++)         {             up = w;             down = 0;///初始化。             for(int j=i+1;j<n+2;j++)             {                 if(p[i].l!=p[j].l)//不在一條豎直線上。                 {                     sum = Math.max(sum,(p[j].l-p[i].l)*(up-down));//更新資料,取最大值                     if(p[j].r>p[i].r)///處於上方,更新上邊界。                     {                         up = Math.min(up,p[j].r);                     }                     else down = Math.max(down,p[j].r);//更新下邊界                 }             }         }         return sum;     }     public static int getr(int n,int l)///這裡和上面一樣,不再作具體的解釋。     {         /*          * 按照縱座標的順序排序          */         Point t = new Point(0,0);         for(int i=0;i<n+2;i++)         {             for(int j=i+1;j<n+2;j++)             {                 if(p[i].r>p[j].r)                 {                     t = p[i];                     p[i] = p[j];                     p[j] = t;                 }                 else if(p[i].r==p[j].r&&p[i].l>p[j].l)                 {                     t = p[i];                     p[i] = p[j];                     p[j] = t;                 }             }         }         int sum = 0;         int up = 0;         int down = 0;         for(int i=0;i<n+2;i++)         {             up = l;             down = 0;             for(int j=i+1;j<n+2;j++)             {                 if(p[i].r!=p[j].r)                 {                     sum = Math.max(sum,(p[j].r-p[i].r)*(up-down));                     if(p[j].l>p[i].l)                     {                         up = Math.min(up,p[j].l);                     }                     else down = Math.max(down,p[j].l);                 }             }         }         return sum;     } }