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) and (A,B) (A,B). FJ builds n n vertical fences ( 0≤n≤25,000 0≤n≤25,000) at distinct locations a1…an a1…an ( 0<ai<A 0<ai<A); each fence runs from (ai,0) (ai,0) to (ai,B) (ai,B). He also builds m m horizontal fences ( 0≤m≤25,000 0≤m≤25,000) at locations b1…bm b1…bm ( 0<bi<B 0<bi<B); each such fence runs from (0,bi) (0,bi) to (A,bi) (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) 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.
INPUT FORMAT (file fencedin.in):
The first line of input contains A A, B B, n n, and m m ( 1≤A,B≤1,000,000,000 1≤A,B≤1,000,000,000). The next n n lines contain a1…an a1…an, and the next m m lines after that contain b1…bm b1…bm.
OUTPUT FORMAT (file fencedin.out):
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++).
SAMPLE INPUT:
15 15 5 2 2 5 10 6 4 11 3
SAMPLE OUTPUT:
44
题意:求最少需要去掉多少长度的栅栏使得每一块都连通
题解:我们可以把每个块看成一个点 那么这道题就变成了最小生成树
但由于数据太大 我们不能建图
假设我们 x 轴删除了 xx 次 y轴删除了 yy 次
在开始时 xx=yy=0
那么第一次删除肯定是删除 m个最小的或者n个最小的 因为要把一列或者一行都联通
我们还可以发现 只要 xx 或者 yy 任意一个为0 那么一行或者一列的栅栏就都要删除
当都不为0时 假设我们接下来要删除 x 轴的 因为已经有 yy 列联通了 所以我们就可以少删除 yy 个 那么就是删除 m+1-yy 个
同理 如果要删除 y 轴的 我们删除 n+1-xx 个即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node{
ll num,lab;
bool operator <(const node& a)const{
return num<a.num;
}
}e[50005];
ll aa[25005],bb[25005];
int main(){
ll a,b,n,m,i,j,mis=0;
scanf("%lld%lld%lld%lld",&a,&b,&n,&m);
for(i=1;i<=n;i++){
scanf("%lld",&aa[i]);
}
aa[n+1]=a;
sort(aa,aa+1+n);
for(i=0;i<=n;i++)aa[i]=aa[i+1]-aa[i];
for(i=1;i<=m;i++){
scanf("%lld",&bb[i]);
}
bb[m+1]=b;
sort(bb,bb+1+m);
for(i=0;i<=m;i++)bb[i]=bb[i+1]-bb[i];
ll cnt=0;
for(i=0;i<=n;i++){
e[++cnt].num=aa[i];
e[cnt].lab=1;
}
for(i=0;i<=m;i++){
e[++cnt].num=bb[i];
e[cnt].lab=0;
}
sort(e+1,e+1+n+m+2);
ll ans=0;
ll xx=0,yy=0,res=(n+1)*(m+1)-1;
for(i=1;i<=n+m+2;i++){
if(e[i].lab){
if(xx==0||yy==0)ans+=e[i].num*m;
else ans+=e[i].num*(m+1-yy);
xx++;
}
else{
if(xx==0||yy==0)ans+=e[i].num*n;
else ans+=e[i].num*(n+1-xx);
yy++;
}
}
cout<<ans<<endl;
return 0;
}