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. 身边的人工智能&人工智能发展史

    智能家具 扫地机器人 智能音箱 个人助手 在线翻译 谷歌翻译 微软翻译 YouTube 视频翻译 图像识别 人脸识别 AI+摄像头 下棋高手 Alphago 2017年打败柯洁 成为世界第一 Alph ...

  2. C++ 一般模板友元关系

    //一般模板友元关系 #include "stdafx.h" #include <iostream> using namespace std; template< ...

  3. [Python ]小波变化库——Pywalvets 学习笔记

    [Python ]小波变化库——Pywalvets 学习笔记 2017年03月20日 14:04:35 SNII_629 阅读数:24776 标签: python库pywavelets小波变换 更多 ...

  4. linux 安装禅道 和 CentOS 7 开放防火墙端口 命令

    linux 安装禅道链接:  https://www.cnblogs.com/maohuidong/p/9750202.html CentOS 7 开放防火墙端口 命令 链接:https://www. ...

  5. tensorflow之最近邻算法实现

    最近邻算法,最直接的理解就是,输入数据的特征与已有数据的特征一一进行比对,最靠近哪一个就将输入数据划分为那一个所属的类,当然,以此来统计k个最靠近特征中所属类别最多的类,那就变成了k近邻算法.本博客同 ...

  6. 微信小程序自定义分享封面

    onShareAppMessage:function(options){ let thas = this; if (options.from === 'button') { // 来自页面内转发按钮 ...

  7. 如何向女朋友介绍MySQL索引

    目录 一.前言 二.正文 三.索引的类型 四.动态查找树 五.B-Tree 1.B-Tree特征 2.B-Tree的查找(select) 3.B-Tree的插入(insert) 4.B-Tree的删除 ...

  8. JS基础——脚本位置、数据类型、函数作用域

    (一)脚本位置 JavaScript是嵌套到浏览器里的脚本语言:可放在3个位置: 1.写在头部(head里) <head>    <meta charset="UTF-8& ...

  9. Python-django入门

    上网流程: Django的安装

  10. 配置window下python3环境

    功能介绍 整理生信小知识库,一些技巧一些知识. 昨天 以下配置环境基于window操作系统,安装python3版本为例,推荐基础版配置. !   METHOD 1 (基础版) 官网下载对应电脑版本的p ...