zoukankan      html  css  js  c++  java
  • 【尺取法】Jurisdiction Disenchantment

    【尺取法】Jurisdiction Disenchantment

    PROBLEM

    时间限制: 1 Sec 内存限制: 128 MB

    题目描述

    The Super League of Paragons and Champions (SLPC) has been monitoring a plot by a corrupt politician to steal an election. In the past week, the politican has used a mind-control technique to enslave the n representatives responsible for choosing the election’s winner. Luckily, the SLPC has managed to recruit you and hence has access to your power to break mind-control. You are able to break mind-control in an axis-aligned rectangle. Unfortunately, your power comes at a steep cost; you get a headache the next day proportional to the size of the rectangle. You do not even want to risk or think about what would happen if you tried to use your power multiple times in one day.
    You have done your research and you know the position that each representative will be standing when the votes are about to be cast. You need to free enough representatives to prevent the politician from having a majority (strictly more than one-half) vote. What is the area of the smallest axis-aligned rectangle that you can affect to do this?

    输入

    The first line of input contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 299, n is odd), the number of representatives. Each of the next n lines of input contains two integers, denoting the x and y coordinates of a representative. It is guaranteed that all coordinates are between −10,000 and +10,000.

    输出

    For each test case, output a single line containing the area of the smallest axis-aligned rectangle containing more than n/2 of the representatives.

    样例输入

    2
    1
    1 1
    3
    0 0
    1 4
    3 2
    

    样例输出

    0
    4
    

    提示

    In the first case, a rectangle containing a single point has an area of 0.
    In the second test case, the rectangle needs to include at least two points. There are two smallest possible
    rectangles; one includes (0, 0) and (1, 4) and the other includes (1, 4) and (3, 2). In either case, the area is 4.

    SOLUTION

    题同 POJ3681 Finding the Rectangle,不同在于POJ3681m是输入的,本题m等于n/2+1,即矩形内最少包含m个点。

    对于此类二维平面上的问题,我们可以先思考简化版本,即在一维直线上至少取m个点,如何使得包含这些点的线段最短?显然贪心的想法就是先只取前m个点,然后在直线上保持这个大小为m个点的窗口(可以用双端队列),使它向右滑动,在这个过程中记录队首和队尾距离的最小值即为答案。这个方法也叫尺取法。

    那么对于本题在二维平面上如何尺取呢,显然可以用降维的方法。先将所有点分别按照x,y大小排序,所以要将坐标数据复制一份。
    然后在x方向上暴力枚举宽度和起始位置,在y方向上尺取并更新答案即可。注意不可以两个方向上都用尺取法。

    时间复杂度 (O(n^3))

    CODE

    #define IN_PC() freopen("C:\Users\hz\Desktop\in.txt","r",stdin)
    #define OUT_PC() freopen("C:\Users\hz\Desktop\out.txt","w",stdout)
     
    #include <bits/stdc++.h>
     
    using namespace std;
    typedef long long ll;
     
    const int MAXN = 300;
     
    struct node {
        int x, y;
    } nd1[MAXN], nd2[MAXN];
     
    int main() {
        //IN_PC();
        int T;
        for (cin >> T; T; T--) {
            int n;
            scanf("%d", &n);
            for (int i = 0; i < n; i++) {
                scanf("%d%d", &nd1[i].x, &nd1[i].y);
                nd2[i] = nd1[i];
            }
            sort(nd1, nd1 + n, [](node a, node b) {
                return a.x < b.x;
            });
            sort(nd2, nd2 + n, [](node a, node b) {
                return a.y < b.y;
            });
            int ans = INT_MAX, m = n / 2 + 1;
            for (int i = 0; i < n; i++) {
                for (int j = i; j < n; j++) {
                    int l = nd1[i].x, r = nd1[j].x;
                    deque<int> dq;
                    for (int k = 0; k < n; k++) {
                        if (nd2[k].x < l || nd2[k].x > r) continue;
                        if (dq.size() < m) dq.push_back(k);
                        if (dq.size() == m) {
                            int d = nd2[dq.front()].y;
                            int u = nd2[dq.back()].y;
                            ans = min(ans,(u-d)*(r-l));
                            dq.pop_front();
                        }
                    }
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    目前流行的源程序版本管理软件和项目管理软件都有哪些, 各有什么优缺点?
    课程不懂的问题
    自我介绍
    .net发送邮件代码示例
    清除HTML中的特殊字符
    Html特殊字符转义处理
    截取字符窜(区分中英文)
    截取需要的字符串(非字节处理)
    区分24小时和12小时的时间格式转换为
    时间戳转为时间
  • 原文地址:https://www.cnblogs.com/NeilThang/p/10686262.html
Copyright © 2011-2022 走看看