Distant Galaxy UVA - 1382

问题

https://vjudge.net/problem/UVA-1382

分析

不能使用暴利的方法枚举,需要O(n^5)的时间,太多了。
只枚举矩形的上下边界,然后通过维护的方法计算最优的左右边界。
这题逻辑比较麻烦,尤其是点是否在交点处

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
typedef long long LL;
const int maxn=100+5;
struct Point{
    int x,y;
    bool operator < (const Point &rhs) const {
        return x<rhs.x;
    }
}point[maxn];
int n,m,y[maxn],on[maxn],on2[maxn],Left[maxn];

int solve(){
    sort(point,point+n);
    sort(y,y+n);
    m=unique(y,y+n)-y;
    if(m<=2) return n;  //最多两种不同的y坐标

    int ans=0;
    for(int a=0;a<m;++a){
        for(int b=a+1;b<m;++b){
            int ymin=y[a],ymax=y[b];
            int k=0;
            for(int i=0;i<n;++i){
                if(i==0 || point[i].x!=point[i-1].x){
                    k++;
                    on[k]=on2[k]=0;
                    Left[k]=(k==0)?0:Left[k-1]+on2[k-1]-on[k-1];  //这地方的计算比较巧妙
                }
                if(point[i].y>ymin && point[i].y<ymax) on[k]++;
                if(point[i].y>=ymin && point[i].y<=ymax) on2[k]++;
            }
            if(k<=2) return n;  //最多两种不同的x坐标
            int M=0;
            for(int j=1;j<=k;++j){
                ans=max(ans,Left[j]+on2[j]+M);
                M=max(M,on[j]-Left[j]);
            }
        }
    }
    return ans;
}

int main(void){
    int kase=0;
    while(scanf("%d",&n)==1 && n){
        for(int i=0;i<n;++i){
            scanf("%d%d",&point[i].x,&point[i].y);
            y[i]=point[i].y;
        }
        printf("Case %d: %d\n",++kase,solve());
    }
    return 0;
}
发布了180 篇原创文章 · 获赞 3 · 访问量 3477

猜你喜欢

转载自blog.csdn.net/zpf1998/article/details/104723309
今日推荐