【尺取法】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;
}

【尺取法】Jurisdiction Disenchantment的更多相关文章

  1. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  2. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  3. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  4. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  5. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  6. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  7. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  8. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  9. HDU 5358 尺取法+枚举

    题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...

随机推荐

  1. 手把手使用Git?

    下载和安装:Git下载和安装教程 学习使用Git:学习Git 安装TortoiseGit:教程 TortoiseGit与Git生成SSH密钥添加到GitHub账号的简单方法:解决方法

  2. 011_TCP专项研究监控

    (1)In Segs 数据源: /proc/net/snmp; 采集方式:累计值,每10秒采集一次取差值: 指标:net.snmp.tcp (key: system); In Segs: tcp协议层 ...

  3. Mysql常用命令大全 sql

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  4. 2018-2019-2 网络对抗技术 20165314 Exp4 恶意代码分析

    一.原理与实践说明 1.实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,s ...

  5. struts2-剩余

    一.说明 类型转换.输入验证(前台和后台)(validate().validateXXX().xml) 标签.上传下载.i18n(国际化).ognl(#reqeust.name) 注解方式.log4j ...

  6. 末学者笔记--NTP服务和DNS服务

    NTP时间服务器 一.概念: 作用:ntp主要是用于对计算机的时间同步管理操作. 时间是对服务器来说是很重要的,一般很多网站都需要读取服务器时间来记录相关信息,如果时间不准,则可能造成很大的影响. 二 ...

  7. Zabbix (五)

    介绍添加主机时,各个参数的含义 https://blog.51cto.com/5001660/2154692 Zabbix配置介绍: https://blog.51cto.com/5001660/21 ...

  8. C#冒泡法排序源码

    如下内容内容是关于C#冒泡法排序的内容,应该对码农有一些用途. int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 }; for( int j ...

  9. 2018-2019-2 20165323《网络攻防技术》Exp5 MSF基础应用

    一.知识点总结 1.MSF攻击方法 主动攻击:扫描主机漏洞,进行攻击 攻击浏览器 攻击其他客户端 2.MSF的六种模块 渗透攻击模块Exploit Modules:攻击漏洞,把shellcode&qu ...

  10. Sublime插件:Terminal

    这几天在window环境下用gulp构建前端工程,切来切去浪费了不少时间(右键sublime菜单打开文件所在目录,然后去项目根目录,右键打开cmder).这点webstorm自带的Terminal真的 ...