LA3695 Distant Galaxy

   对于这种题真的是一点想法都没有,根本不知道从哪里下手。。。

   还是大白上的题(P52),有详解。

   书上说的枚举上下边界是指求出总共有多少种不同的纵坐标的值,因为题目中给出的坐标都是整数,每个纵坐标值都可以做为一个边界,这样就只有有限的组合。

   还有一开始很困惑的地方就是,为什么求出的矩形都是特殊的矩形----与坐标轴平行的。值得仔细想想

   

   

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 105;
typedef long long LL;
struct point
{
    int x;
    int y;
    friend bool operator <(point a,point b)
    {
        return a.x<b.x;
    }
}a[maxn];
int y[maxn],on[maxn],on2[maxn],left[maxn];
int solve(int n)
{
    int m = unique(y,y+n)-y;
    if(m<=2)return n;
    int ans = 0;
    for(int low = 0;low<m;++low)for(int up = low+1;up<m;++up)
    {
        int ylow = y[low],yup = y[up],k = 0;
        for(int i = 0;i<n;++i)
        {
            if(i==0 || a[i].x!=a[i-1].x)
            {
                k++;
                on[k] = on2[k] = 0;
                if(k==1)left[k] = 0;
                else left[k] = left[k-1]+on2[k-1]-on[k-1];
            }
            if(a[i].y>ylow && a[i].y<yup)on[k]++;
            if(a[i].y>=ylow && a[i].y<=yup)on2[k]++;
        }
        if(k<=2)return n;
        int t = 0;
        for(int i = 1;i<=k;++i)
        {
            ans = max(ans,left[i]+on2[i]+t);
            t = max(t,on[i]-left[i]);
        }
    }
    return ans;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int n,kase = 0;
    while(~scanf("%d",&n) && n)
    {
        for(int i =0;i<n;++i)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            y[i] = a[i].y;
        }
        sort(y,y+n);
        sort(a,a+n);
        printf("Case %d: %d\n",++kase,solve(n));
    }

    return 0;
}

   

时间: 2024-10-11 23:35:17

LA3695 Distant Galaxy的相关文章

LA 3695 Distant Galaxy

给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on[i]表示位于竖线i上两条水平线之间(并不在水平线上)的点数,on2[i]表示位于竖线i上两条水平线之间加上水平线边界上的点数. 所以矩形框上的点数为: left[j]-left[i]+on[i]+on2[j] 枚举右边界竖线j,j确定后维护on[i]-left[i]的最大值. 1 //#defin

UVA - 1382 Distant Galaxy

题目大意:给出 n 个点,问说一个平行与 x 轴和 y 轴的矩形,最多能有多少个点落在边上. 解题思路:首先先将 y 轴相同的放在一起,然后枚举左右边界,考虑上下边界,维护最大值 #include <cstdio> #include <algorithm> using namespace std; struct Point { int x; int y; bool operator < (const Point& a) const { return x < a.

【poj3141】 Distant Galaxy

http://poj.org/problem?id=3141 (题目链接) 题意:给出平面上n个点,找出一个矩形,使边界上包含尽量多的点. solution  不难发现,除非所有输入点都在同一行或同一列上,最优矩形的4条边上都至少有一个点.这样的话,我们可以枚举四条边穿过的点,然后统计点数.  考虑部分枚举,只枚举矩形上下界,用其它方法确定左右界.  设一条竖线i,用left[i]表示竖线左边位于上下界上单点数(不统计位于该竖线上的点),on[i]和on2[i]表示竖线上位于上下边界之间的点数(

UVA - 1312 Cricket Field 构造

题目大意:在一个W * H的网格中有n棵树,要求你在这个网格中找出最大个的一个正方形,这个正方形内部不能有树,边上可以有树 解题思路:刚开始以为要暴力枚举每一个点,结果发现错了,其实这题就像UVA - 1382 Distant Galaxy这题一样,只不过这个是要找正方形,找正方形和找矩形类似,只需要取矩形的最小边就可以了 #include<cstdio> #include<algorithm> using namespace std; #define maxn 110 struc

ThoughtWorks笔试题之Merchant&#39;s Guide To The Galaxy解析

一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天HR就打开电话,基本了解了一下情况(工作环境不错,男人妹子比例:1:1,双休,六险一金,满一年年假15天,病假8天,月薪1W--2W).然后立马收到一封:Coding Assignment的笔试题目.网上搜索了一下,发现这个公司还是挺大的,公司面试流程是出了名的繁杂和苛刻.据说有8轮:电话面试=>笔试=>Homework=>结对编程(中午管饭)=>技术面试=>PM面试=>

HDU 5073 Galaxy (数学)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 4991    Accepted Submission(s): 1215Special Judge Problem Description Good news for us: to release the financial pressure, the government

HDU 5073 Galaxy(居然是暴力)

Problem Description Good news for us: to release the financial pressure, the government started selling galaxies and we can buy them from now on! The first one who bought a galaxy was Tianming Yun and he gave it to Xin Cheng as a present. To be fashi

POJ3525-Most Distant Point from the Sea(二分+半平面交)

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3955   Accepted: 1847   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t

la----3695 City Game(最大子矩阵)

Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees, factories and buildings. There is still some space in the area that is