Ultraviolet-1382 Distant Galaxy

Source: Internet
Author: User

Ultraviolet-1382 Distant Galaxy

N vertices are given, and a rectangle parallel to the x and y axes can have at most many vertices on the edge.

Solution: first, put the y axis together, then enumerate the left and right borders, consider the upper and lower boundary, and maintain the maximum value.

#include 
  
   #include using namespace std;struct Point {    int x;    int y;    bool operator < (const Point& a) const {        return  x < a.x;    }};const int maxn = 100 + 10;Point P[maxn];int y[maxn], on[maxn], on2[maxn], left[maxn];int solve(int N) {    sort(P, P + N);    sort(y, y + N);    int m = unique(y, y + N) - y;    if (m <= 2) return N;    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 || P[i].x != P[i-1].x) {                    k++;                    on[k] = on2[k] = 0;                    left[k] = left[k-1] + on2[k-1] - on[k-1];                }                if (P[i].y > ymin && P[i].y < ymax) on[k]++;                if (P[i].y >= ymin && P[i].y <= ymax) on2[k]++;            }            if (k <= 2) return N;            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() {    int N, kase = 0;    while (scanf(%d, &N), N) {        for (int i = 0; i < N; i++) {            scanf(%d%d, &P[i].x, &P[i].y);            y[i] = P[i].y;        }        printf(Case %d: %d, ++kase, solve(N));    }    return 0;}
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.