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

Input
1
2
2 2 3 3
4 4 5 5
Output
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 ;
}

G - Radar Scanner Gym - 102220G(中位数~~)的更多相关文章

  1. Radar Scanner Gym - 102220G

    题目链接:https://vjudge.net/problem/Gym-102220G 题意:在水平直角坐标系中有n个矩形,你可以将矩形沿着平行于X轴和Y轴水平移动,问至少经过几次移动可以使得所有的矩 ...

  2. G - WiFi Password Gym - 101608G (异或思维题+曲尺)

    题目链接:https://cn.vjudge.net/contest/285962#problem/G 题目大意:给你n和m,n代表有n个数,然后让你找出一个最长的区间,使得这个区间内的所有的数的‘’ ...

  3. G - Green-Red Tree Gym - 102190G

    题目链接:http://codeforces.com/gym/102190/attachments 题解:我们先将前5个点分别涂上红色或者绿色,使得这两棵树在5个点中都是连通,并不存在自环(建边方式不 ...

  4. The 13th Chinese Northeast Collegiate Programming Contest(B C E F H J)

    B. Balanced Diet 思路:把每一块选C个产生的价值记录下来,然后从小到大枚举C. #include<bits/stdc++.h> using namespace std; ; ...

  5. The 13th Chinese Northeast Collegiate Programming Contest

    题解: solution Code: A. Apple Business #include<cstdio> #include<algorithm> #include<ve ...

  6. [Swift]Scanner字符串扫描类

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. 2018 Multi-University Training Contest 3 - HDU Contest

    题解: solution Code: A. Ascending Rating #include<cstdio> const int N=10000010; int T,n,m,k,P,Q, ...

  8. 2018 Multi-University Training Contest 3 Solution

    A - Problem A. Ascending Rating 题意:给出n个数,给出区间长度m.对于每个区间,初始值的max为0,cnt为0.遇到一个a[i] > ans, 更新ans并且cn ...

  9. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

随机推荐

  1. SAP HANA学习笔记

    SAP HANA:High-Performance Analytic ApplianceSAP HANA XSC:Extended Application Services Classic(SAP推出 ...

  2. VUE.js入门学习(3)-深入理解VUE组建

    1.使用组件的细节点 (1)is="模版名" (2)在子组建定义data的时候,data必须是一个函数,而不能是一个对象,每个子组建都有自己的数据存储.之间不会相互影响. (3)操 ...

  3. Linux基础应用

    Linux刚面世时并没有图形界面,因此所有的操作全靠命令完成,如磁盘操作.文件读取.目录操作.进程管理.文件权限等都要通过命令完成.且在职场中,大量的服务器维护都是通过远程命令来完成. 常用的7个命令 ...

  4. OC中浮点数转整数的进一法和去尾法

    //去尾法,最小去尾单位为0.000001 floorf(4.1)4 floorf(4.9)4 floorf(4.999999)4 floorf(4.9999999)5 //进一法,最小进位单位为0. ...

  5. bash字符串处理

    将movie目录下的文件名写到markdown文件中 , 再转html rm index.md ; for f in `find . *.* | sort`; do [ -f $f ] &&a ...

  6. CAD快捷键大全

  7. Linux--Centos安装VNC

    参考:https://linux.cn/article-5335-1.html 安装 vnc 服务器 yum install tigervnc-server -y 配置 vnc .service 更改 ...

  8. 超级详细通信协议解析webservice和dubbo通信协议区别

    简单说下接触webservice的背景吧,因为之前的接口对接更多的是成熟的接口品牌像是阿里巴巴.腾讯.聚合数据等,他们接口规范一般都是基于restful进行接口对接.什么是restful接口,可以通过 ...

  9. 1. rabbitmq 安装

    1. ubuntu 16 18 安装 https://blog.csdn.net/haeasringnar/article/details/82715823 2. centos 7 https://w ...

  10. JQuery与JS比较

    备注:原文转自脚本之家,原文地址:http://www.jb51.net/article/39494.htm     转载仅为方便统一整理收藏内容,别无他意 JQuery与JS的比较: jquery ...