SDUT 2401 最大矩形面积
生活随笔
收集整理的這篇文章主要介紹了
SDUT 2401 最大矩形面积
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2401
話說GSB 出的這個題確實不大好想,給定題意不說了。
才開始我們想的是從左到右,從上往下走一邊,找相鄰連點兩重循環找最大面積,敲出來一交WA,郁悶,才開始我們都認為這個辦法對,想了很長時間,到最后ZC出了各情況直接給否定了,主要是我們沒有考慮上下界問題。同樣只要考慮好上下界,還是按那個思路左右上下走一邊,最后求出最大值即可。
View Code #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 1007 using namespace std;struct node {int x,y; }p[maxn]; int n,tal,l,w; int cmpx(node a,node b) {if (a.x != b.x) return a.x < b.x;else return a.y < b.y; } int cmpy(node a,node b) {if (a.y != b.y) return a.y < b.y;else return a.x < b.x; } int LR() {int i,j,ans;ans = 0;for (i = 0; i < tal; ++i){int top = w, down = 0;for (j = i + 1; j < tal; ++j){if (p[i].x != p[j].x){ans = max(ans,(p[j].x - p[i].x)*(top - down));//更新上下界if (p[j].y > p[i].y) top = min(top,p[j].y);else down = max(down,p[j].y);}}}return ans; } int UD() {int i,j,ans;ans = 0;for (i = 0; i < tal; ++i){int L = 0, R = l;for (j = i + 1; j < tal; ++j){if (p[i].y != p[j].y){ans = max(ans,(p[j].y - p[i].y)*(R - L));//更新左右界if (p[j].x > p[i].x) R = min(R,p[j].x);else L = max(L,p[j].x);}}}return ans; } int main() {//freopen("data.in","r",stdin);int i,t;scanf("%d",&t);while (t--){scanf("%d%d",&l,&w);scanf("%d",&n);if (n == 0){printf("%d\n",l*w);continue;}//作為結束的邊界p[0].x = p[0].y = 0;p[1].x = l; p[1].y = w;tal = 2;for (i = 0; i < n; ++i){scanf("%d%d",&p[tal].x,&p[tal].y);tal++;}//按X軸排序,從左到右查找sort(p,p+tal,cmpx);int l = LR();//按Y軸排序從上往下查找sort(p,p+tal,cmpy);int d = UD();//取最大值printf("%d\n",max(l,d));}return 0; }轉載于:https://www.cnblogs.com/E-star/archive/2012/04/23/2467028.html
總結
以上是生活随笔為你收集整理的SDUT 2401 最大矩形面积的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是 PureMVC 框架(提供下载)
- 下一篇: cookie完全跨域