G - Radar Scanner Gym - 102220G(中位数~~)
zThere are n rectangle radar scanners on the ground. The sides of them are all paralleled to the axes. The i-th scanner's bottom left corner is square (ai,bi) and its top right corner is square (ci,di)
. Each scanner covers some squares on the ground.
You can move these scanners for many times. In each step, you can choose a scanner and move it one square to the left, right, upward or downward.
Today, the radar system is facing a critical low-power problem. You need to move these scanners such that there exists a square covered by all scanners.
Your task is to minimize the number of move operations to achieve the goal.
Input
The first line of the input contains an integer T(1≤T≤1000)
, denoting the number of test cases.
In each test case, there is one integer n(1≤n≤100000)
in the first line, denoting the number of radar scanners.
For the next n
lines, each line contains four integers ai,bi,ci,di(1≤ai,bi,ci,di≤109,ai≤ci,bi≤di)
, denoting each radar scanner.
It is guaranteed that ∑n≤106
.
Output
For each test case, print a single line containing an integer, denoting the minimum number of steps.
Example
1
2
2 2 3 3
4 4 5 5
2 题解:由于横纵方向地位相同,我们不妨来看横方向,题目要找一点x使得n条线段经过平移最少次数,至少重合一点。
假设那一点就为x,那么一条线段至少与x有交点的话,所需距离为:d=(|l-x|+|r-x|-|r-l|)/2,纸上画一遍即可。我们要找的是所有线段移动的距离之和最小,那么只需Σd最小,
由于d中的|r-l|为常数,所以我们只需要求Σ(|l-x|+|r-x|)最小,那么x就是所有l,r的中位数了~~。题目难得就是转化~~
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=;
struct node
{
ll l,r;
}q[maxn],w[maxn];
ll n,a[maxn*];
ll ok(node q[])
{
int top=;
for(int i=;i<=n;i++){
a[++top]=q[i].l;
a[++top]=q[i].r;
}
sort(a+,a++top);
ll x=(a[top/]+a[top/+])/;
ll ans=;
for(int i=;i<=n;i++){
ans+=(abs(q[i].l-x)+abs(q[i].r-x)-(q[i].r-q[i].l))/;
}
return ans;
}
int main()
{
ios::sync_with_stdio();
int T;
cin>>T;
while(T--){
cin>>n;
for(int i=;i<=n;i++){
cin>>q[i].l>>w[i].l>>q[i].r>>w[i].r;
}
ll ans=;
ans+=ok(q);
ans+=ok(w);
cout<<ans<<endl;
}
return ;
}
最新文章
- mina通信 demo
- python常用文件处理函数_1
- Cocoapods的使用教程
- NYOJ题目596谁是最好的Coder
- linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发
- JS动画理论
- 利用CSS实现居中对齐
- Dining(最大流)
- UVALive 7070 The E-pang Palace(暴力)
- 201621123031 《Java程序设计》第12周学习总结
- 设备唯一标识方法(Unique Identifier):如何在Windows系统上获取设备的唯一标识 zz
- laravel+Redis简单实现队列通过压力测试的高并发处理
- 通过RequestContextHolder直接获取HttpServletRequest对象
- 【源码解读】EOS测试插件:txn_test_gen_plugin.cpp
- 喵哈哈村的魔法考试 Round #21 (Div.2) 题解
- Linux下ip地址查询
- HDU1029(KB12-B)
- 浅谈iOS 自动调节文本高度
- 升级项目到Vs2010,编译时出现:MSB6006: “LC.exe”已退出,解决方法
- Splash images_enabled 属性
热门文章
- Mac Eclipse 打包可执行jar文件
- Codeforces 1299A/1300C - Anu Has a Function
- ubuntu下面嘚一些常用基本命令
- 日志处理中一些shell命令技巧
- Java8集合框架——HashMap源码分析
- Python的 5 种高级用法,效率提升没毛病!
- java8 String intern()
- SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&;2-3 逻辑运算符
- “杀死”纸质名片!HiHello能重构商业关系网吗?
- UOJ #2 【NOI2014】起床困难综合症