题面

Time Limit: 10 Sec Memory Limit: 128 MB

Submit: 136 Solved: 90

[Submit][Status][Discuss]

Description

The cross-country skiing course at the winter Moolympics is described by an M x N grid of elevations (1 <= M,N <= 500), each elevation being in the range 0 .. 1,000,000,000. Some of the cells in this grid are designated as starting points for the course. The organizers of the Moolympics want to assign a difficulty rating to each starting point. The difficulty level of a starting point P should be the minimum possible value of D such that a cow can successfully reach at least T total cells of the grid (1 <= T <= MN), if she starts at P and can only move from cell to adjacent cell if the absolute difference in elevation between the cells is at most D. Two cells are adjacent if one is directly north, south, east, or west of the other. Please help the organizers compute the difficulty rating for each starting point.

给定一个n*m的高度矩阵,一个点的难度P是指 最小的D使得以这个点为起点,可到达的格子数量至少为T,一个格子可以到另外一个格子必须保证两个格子相邻且高度差不超过D,相邻是指4-相邻。再给定一个同样大小的01矩阵,求出所有1处格子的难度和。

Input

  • Line 1: The integers M, N, and T.

  • Lines 2..1+M: Each of these M lines contains N integer elevations.

  • Lines 2+M..1+2M: Each of these M lines contains N values that are either 0 or 1, with 1 indicating a cell that is a starting point.

Output

  • Line 1: The sum of difficulty ratings of all starting points (note that this may not fit into a 32-bit integer, even though individual difficulty ratings will).

Sample Input

3 5 10

20 21 18 99 5

19 22 20 16 17

18 17 40 60 80

1 0 0 0 0

0 0 0 0 0

0 0 0 0 1

Sample Output

24

OUTPUT DETAILS: The difficulty rating of the upper-left starting point is 4, and for the lower-right it is 20.

解题思路

  首先这道题肯定要用并查集来维护连通性,按照边权排序后一条一条的加边,在并查集中还要维护一下每个联通块的大小和联通块内起点的数量,因为每个点只会被更新一次,所以当两个联通块合并时大小\(>=t\),而合并前没有时就统计答案。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std;
const int MAXN = 505*505;
typedef long long LL; inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
} int n,m,siz[MAXN],fa[MAXN],t,cnt,num,a[505][505],sum[MAXN];
LL ans; struct Edge{
int u,v,w;
friend bool operator<(const Edge A,const Edge B){
return A.w<B.w;
}
}edge[MAXN<<2]; inline int calc(int x,int y){
return (x-1)*m+y;
} inline void add(int x,int y,int w){
edge[++cnt].u=x;edge[cnt].v=y;edge[cnt].w=w;
} int find(int x){
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
} int main(){
n=rd(),m=rd(),t=rd();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
a[i][j]=rd();
if(i!=1) add(calc(i-1,j),calc(i,j),abs(a[i][j]-a[i-1][j]));
if(j!=1) add(calc(i,j-1),calc(i,j),abs(a[i][j]-a[i][j-1]));
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
fa[calc(i,j)]=calc(i,j),siz[calc(i,j)]=rd(),sum[calc(i,j)]=1;
sort(edge+1,edge+1+cnt);
for(int i=1;i<=cnt;i++){
int u=find(edge[i].u),v=find(edge[i].v);
if(u==v) continue;
if(sum[u]<t && sum[u]+sum[v]>=t) ans+=(LL)siz[u]*edge[i].w;
if(sum[v]<t && sum[u]+sum[v]>=t) ans+=(LL)siz[v]*edge[i].w;
fa[u]=v;siz[v]+=siz[u];sum[v]+=sum[u];
}
printf("%lld\n",ans);
return 0;
}

BZOJ 3430: [Usaco2014 Jan]Ski Course Rating(并查集+贪心)的更多相关文章

  1. bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  2. 【BZOJ】【3673】可持久化并查集 & 【3674】可持久化并查集加强版

    可持久化并查集 Orz hzwer & zyf 呃学习了一下可持久化并查集的姿势……其实并查集就是一个fa数组(可能还要带一个size或rank数组),那么我们对并查集可持久化其实就是实现一个 ...

  3. bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】

    bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...

  4. BZOJ 3432: [Usaco2014 Jan]Cross Country Skiing (二分+染色法)

    还是搜索~~可以看出随着D值的增大能到达的点越多,就2分d值+染色法遍历就行啦~~~ CODE: #include<cstdio>#include<iostream>#incl ...

  5. BZOJ 3433 [Usaco2014 Jan]Recording the Moolympics:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3433 题意: 给出n个区间[a,b). 有两个记录器,每个记录器中存放的区间不能重叠. 求 ...

  6. 【bzoj 3433】{Usaco2014 Jan} Recording the Moolympics(算法效率--贪心)

    题意:给出n个区间[a,b),有2个记录器,每个记录器中存放的区间不能重叠.求2个记录器中最多可放多少个区间. 解法:贪心.只有1个记录器的做法详见--关于贪心算法的经典问题(算法效率 or 动态规划 ...

  7. 【BZOJ】1015: [JSOI2008]星球大战starwar(并查集)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1015 看了题解的囧T_T,一开始以为是求割点,但是想到割点不能统计.... 这题用并查集,思想很巧妙 ...

  8. BZOJ 1050 旅行comf(枚举最小边-并查集)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1050 题意:给出一个带权图.求一条s到t的路径使得这条路径上最大最小边的比值最小? 思路 ...

  9. bzoj 4423 [AMPPZ2013]Bytehattan(对偶图,并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4423 [题意] 给定一个平面图,随时删边,并询问删边后两点是否连通.强制在线. [科普 ...

随机推荐

  1. 微信JS-SDK接口上传图片以及wx.config的配置

    最近做的微信网页要实现一个上传图片的功能,倒腾了半天终于搞好了,具体的步骤可以查看微信官方文档https://developers.weixin.qq.com/doc/offiaccount/OA_W ...

  2. LOJ 2554 「CTSC2018」青蕈领主——结论(思路)+分治FFT

    题目:https://loj.ac/problem/2554 一个“连续”的区间必然是一个排列.所有 r 不同的.len 最长的“连续”区间只有包含.相离,不会相交,不然整个是一个“连续”区间. 只有 ...

  3. VIEW当中自定义属性的使用

    主要有三种方法可以实现自定义属性. 第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值. (1)在xml文件中设置属性值 [html] vie ...

  4. LINUXE下执行php 定时任务

    linux test.php <?php $fn='/home/root.adminssh/boz/logs'; $data=rand(1,9999); $fp=fopen($fn,'wb'); ...

  5. HTML5: HTML5 内联 SVG

    ylbtech-HTML5: HTML5 内联 SVG 1.返回顶部 1. HTML5 内联 SVG HTML5 支持内联 SVG. 什么是SVG? SVG 指可伸缩矢量图形 (Scalable Ve ...

  6. 发送邮件 django

    https://blog.csdn.net/qq_39138295/article/details/82527868 https://www.cnblogs.com/yoyoketang/p/1048 ...

  7. 使用python实现希尔、计数、基数排序

    希尔排序 希尔排序是一个叫希尔的数学家提出的一种优化版本的插入排序. 首先取一个整数d1=n//2,将元素分为d1个组,每组相邻元素之间的距离为d1,在各组内进行直接插入排序. 取第二个整数d2=d1 ...

  8. dir(dict)|字典的创建-添加-删除-修改-判断存在-取值等相关操作

    dir(dict) ####字典操作:创建-添加-删除-修改-判断存在-取值 #(一)创建字典: {} .等号. zip(). [(),()] #1.创建空字典 dict0 = {} #2.等号创建 ...

  9. CF_127E reader Display

    这道题其实找到规律之后其实不难,和破损的键盘一样,都有点递推转移的感觉 题意: 你可以进行这样一次操作:选一个点,然后把这个点横切竖切直到正对角线上面的点,全部翻转过来,问你要进行多少次操作才能把所有 ...

  10. 转 Python - openpyxl 读写操作Excel

    Python - openpyxl 读写操作Excel   openpyxl特点   openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间 ...