题目描述

Farmer John has realized that many of his cows are strangely agoraphobic (being fearful of large open spaces). To try and make them less afraid of grazing, he partitions his large field into a number of smaller regions by building vertical (north-south) and horizontal (east-west) fences.

The large field is a rectangle with corner points at (0,0)(0,0)(0,0) and (A,B)(A,B)(A,B) . FJ builds nnn vertical fences ( 0≤n≤25,0000 \leq n \leq 25,0000≤n≤25,000 ) at distinct locations a1…ana_1 \ldots a_na1​…an​ ( 0<ai<A0 < a_i < A0<ai​<A ); each fence runs from (ai,0)(a_i, 0)(ai​,0) to (ai,B)(a_i, B)(ai​,B) . He also builds mmm horizontal fences ( 0≤m≤25,0000 \leq m \leq 25,0000≤m≤25,000 ) at locations b1…bmb_1 \ldots b_mb1​…bm​ ( 0<bi<B0 < b_i < B0<bi​<B ); each such fence runs from (0,bi)(0, b_i)(0,bi​) to (A,bi)(A, b_i)(A,bi​) . Each vertical fence crosses through each horizontal fence, subdividing the large field into a total of (n+1)(m+1)(n+1)(m+1)(n+1)(m+1) regions.

Unfortunately, FJ completely forgot to build gates into his fences, making it impossible for cows to leave their enclosing region and travel around the entire field! He wants to remedy this situation by removing pieces of some of his fences to allow cows to travel between adjacent regions. He wants to select certain pairs of adjacent regions and remove the entire length of fence separating them; afterwards, he wants cows to be able to wander through these openings so they can travel anywhere in his larger field.

For example, FJ might take a fence pattern looking like this:

+---+--+
| | |
+---+--+
| | |
| | |
+---+--+

and open it up like so:

+---+--+
| |
+---+ +
| |
| |
+---+--+

Please help FJ determine the minimum total length of fencing he must remove to accomplish his goal.

有一个平面,左下角是(0,0),右上角是(A,B)。

有n个平行于y轴的栅栏a1..an,表示挡在(ai,0)到(ai,B)之间。

有m个平行于x轴的栅栏b1..bn,表示挡在(0,bi)到(A,bi)之间。

这样,平面被划成了(n+1)*(m+1)块。

现在要去掉某些栅栏的一部分,使得每一块都连通。

比如原来是这样:

+---+--+

| | | +---+--+

   

+---+--+

可以去掉后变成这样:

+---+--+

| |
+---+ +

 

+---+--+

求最少需要去掉多少长度的栅栏使得每一块都连通。

输入输出格式

输入格式:

The first line of input contains AAA , BBB , nnn , and mmm

( 1≤A,B≤1,000,000,0001 \leq A, B \leq 1,000,000,0001≤A,B≤1,000,000,000 ). The next nnn lines contain a1…ana_1 \ldots a_na1​…an​ ,

and the next mmm lines after that contain b1…bmb_1 \ldots b_mb1​…bm​ .

输出格式:

Please write the minimum length of fencing FJ must remove. Note that this might

be too large to fit into a standard 32-bit integer, so you may need to

use 64-bit integer types (e.g., "long long" in C/C++).

输入输出样例

输入样例#1:

15 15 5 2
2
5
10
6
4
11
3
输出样例#1:

44

Solution:

  本题好考思维啊!

  题意是一个$A*B$的矩形,被$n$条平行于$y$轴的直线和$m$条平行于$x$轴的直线,分为$(n+1)*(m+1)$个矩形,现在要删去长度最小的边(任意两个矩形之间的某条边),使得原矩形内部连通。

  我们可以先画画图,不难归纳出,要使矩形内部连通至少需要删去$(n+1)*(m+1)-1$条边,这个式子可以理解为每个小矩形要与其它小矩形互通,至少删去$1$条边,而只要使$(n+1)*(m+1)-1$个矩形互通,就能使大矩形内部互通(因为最后一个小矩形必定有条边在之前的矩形中已被删去)

  那么我们很快可以有个初步的思路,我们可以先处理出所有的$n+1$条平行$x$轴的边,和$m+1$条平行于$y$轴的边(然后直接$n++,\;m++$),贪心的想到,先删去当前最短的一条边,若是平行于$x$轴就删去$m-j+1$条该边,若是平行于$y$轴就删去$n-i+1$条该边($i$为所有平行于$x$轴的边从小到大排序后的编号,$j$含义类似),为什么是$m-j+1$和$n-i+1$呢?因为我们每次删边后,与其垂直的边就能少删$1$条,具体来说:

  1、当$x[i]<y[j]$的时候,肯定要删第$i$列的水平栅栏,该列的栅栏个数为$m$(行数)$-j$(已经删了多少行)$+1$,对列有影响的是行(列与列不相交)

  2、当$x[i]>y[j]$的时候,肯定要删第$j$行的竖直栅栏,该行的栅栏个数为$n$(列数)$-i$(已经删了多少列)$+1$,对行有影响的是列(行与行不相交)

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
using namespace std;
ll A,B,ans,n,m,x[],y[],a[],b[];
il ll gi(){
ll a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
}
int main(){
A=gi();B=gi();n=gi();m=gi();
For(i,,n)a[i]=gi();
For(i,,m)b[i]=gi();
sort(a+,a+n+),sort(b+,b+m+);
For(i,,n)x[i]=a[i]-a[i-];x[n+]=A-a[n++];
For(i,,m)y[i]=b[i]-b[i-];y[m+]=B-b[m++];
sort(x+,x+n+),sort(y+,y+m+);
ans=x[]*(m-)+y[]*(n-);
for(int i=,j=;i<=n&&j<=m;)
if(x[i]<y[j])ans+=x[i++]*(m-j+);
else ans+=y[j++]*(n-i+);
cout<<ans;
return ;
}

P3141 [USACO16FEB]围栏Fenced In_Platinum的更多相关文章

  1. [USACO16FEB]围栏Fenced In Platinum

    题目:洛谷P3141. 题目大意:有一个方形区域,被分成若干区域.现在要去掉若干条围栏,使得所有区域连通,求最少去掉多少长度的围栏. 解题思路:贪心.建议画图思考. 先对围栏位置进行排序,然后相邻两条 ...

  2. 地理围栏算法解析(Geo-fencing)

    地理围栏算法解析 http://www.cnblogs.com/LBSer/p/4471742.html 地理围栏(Geo-fencing)是LBS的一种应用,就是用一个虚拟的栅栏围出一个虚拟地理边界 ...

  3. 【GPS】 数据围栏

    1.记录gps信息,定位类型  gps  agps ,偏移量 2.根据id检索用户 gps 历史记录 3.创建围栏 4.围栏内用户检索(先实现 圆形和矩形) 5.判断一个点是否进出围栏 应用场景: o ...

  4. iOS地理围栏技术的应用

    遇到一个需求,要求监测若干区域,设备进入这些区域则要上传数据,且可以后台监测,甚至app被杀死也要监测.发现oc的地理围栏技术完美匹配这个需求,任务做完了,把遇到的坑记录下来,也许能帮到你呢. 要做这 ...

  5. 【Android Developers Training】 106. 创建并检测地理围栏

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(1)

    本文描述了一个系统,功能是评价和抽象地理围栏(Geo-fencing),以及监控和分析核心地理围栏中业务的表现. 技术栈:Spring-JQuery-百度地图WEB SDK 存储:Hive-Elast ...

  7. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(2)-查询实现

    在上一篇博客中,我们准备好了数据.现在数据已经以我们需要的格式,存放在Elasticsearch中了. 本文讲述如何在Elasticsearch中进行空间GEO查询和聚合查询,以及如何准备ajax接口 ...

  8. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(3)-前端实现

    转载自:http://www.cnblogs.com/Auyuer/p/8086975.html MoonLight可视化订单需求区域分析系统实现功能: 在现实生活中,计算机和互联网迅速发展,人们越来 ...

  9. Python一行代码处理地理围栏

    最近在工作中遇到了这个一个需求,用户设定地理围栏,后台获取到实时位置信息后通过与围栏比较,判断是否越界等. 这个过程需要用到数据协议为GEOjson,通过查阅资料后,发现python的shapely库 ...

随机推荐

  1. 11_1_GUI

    11_1_GUI 1. AWT AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface ...

  2. spring-bean(注解方式-管理及依赖注入)

    Bean管理(注解方式) 1.添加注解的依赖包:Spring-aop.jar 2.配置spring的XML文件的引入(查官方源码) 3.开启注解的扫描 <context:component-sc ...

  3. turtle画玫瑰花

    import turtle turtle.screensize(400, 300, "pink") turtle.setup(1000, 600) turtle.write('作者 ...

  4. [转] vim配置python自动补全

    vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.pyt ...

  5. JQ常用方法(哈哈)

    1ajax请求 $(function(){   $("#send").click(function(){     $.ajax({     type:"get" ...

  6. 【php】关于trim,rtrim,ltrim,substr 的字符串切割导致 json,_encode无法 识别数据的问题

    示例 <?php $a = rtrim('南宁 .',' .'); echo $a; //输出 南�� echo json_encode($a); //输出空白 $b = ['name'=> ...

  7. git重新下载项目

    file-new-project from version control - git 修改网址为需要的网址

  8. TCP重组问题

    今天问题: vqmon 测试一pcap抓包文件18.pcap.发现实际输出的视频分片信息和抓包不符合. ===>pts : 00:00:33 Too much data in TCP recei ...

  9. 使用Matrix-tree与它的行列式来解决生成树计数问题

    我又把Matrix写错啦 这东西讲课的时候竟然一笔带过了,淦 好吧这东西我不会证 那我们来愉快的看结论吧 啦啦啦 预备工作 你有一个 $ n $ 个点的图 比如说 5 /|\ / | \ 2--1-- ...

  10. 批量上传图片(jQuery-File-Upload使用)

    jQuery-File-Upload jQuery-File-Upload是一个jquery下的ajax文件上传插件,支持批量上传,github地址:https://github.com/blueim ...