题目描述

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.

滑雪场用一个M*N(1 <= M,N <= 500)的数字矩阵表示海拔高度,每个数字表示一个范围在0 .. 1,000,000,000的高度。有些格子被指定为起点,组织者想对这些起点做难度评级。

如果起点P点是一个难度级别为D的起点,则D必须是满足以下条件的一个最小值:

(1)从一个格子只能滑到相邻的格子;

(2)这两个格子的海拔差不超过D;

(3)至少能够到达T(1 <= T <= M*N)个格子(包括起点本身)。

输入输出格式

输入格式:

* 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.

输出格式:

* 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).

输入输出样例

输入样例#1:

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
输出样例#1:

24

说明

The ski course is described by a 3 x 5 grid of elevations. The upper-left and lower-right cells are designated as starting points. From each starting point, we must be able to reach at least 10 cells.

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


woc我要骂人, 这sb题调了我2小时。

日了,不知道哪里的玄学错误找错找了2小时, 最后重写了一边才A。

吐槽完了ri....

一看这道题...水...暴力二分+bfs结果光荣tle;

emm...

我们把每个点与它下边和右边的点连起来,边权是他们的高度差,然后按照边权排序,不停的合并两个端点的集合。

一旦发现两个集合合并之后,它的siz>T,那么立即更新答案,但是以前更新过的集合不需要加入打哪中。

然后再按秩合并。

我们维护每一个集合的siz, num(是这个集合里有多少个出发点);

然后更新答案的时候, 如果之前的那个小集合没有更新过答案,那么ans += ed[i].val * num[x];

因为我们边权是从小到达排序的,所以不用担心得到错误的答案,使得这个集合大小第一次大于T的边,一定是满足题意的最小边。


附上折磨了我一晚上的代码...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
inline int read(){
int res=;char ch=getchar();bool fl=;
while(!isdigit(ch)){if(ch=='-')fl=;ch=getchar();}
while(isdigit(ch)){res=(res<<)+(res<<)+(ch^);ch=getchar();}
return fl?-res:res;
}
int dx[]={, , }, dy[]={, , };
int n, m, T;
int cnt, tot;
long long ans;
struct edge{
int x, y, val;
bool operator < (edge b) {return val < b.val;}
}ed[];
int h[][];
int fa[], sz[];
int id[][], num[];
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);} signed main()
{
n = read(), m = read(), T = read();
tot = ;
for (register int i = ; i <= n ; i ++)
{
for (register int j = ; j <= m ; j ++)
{
h[i][j] = read();
tot+=;
id[i][j] = tot;
fa[id[i][j]] = id[i][j];
sz[id[i][j]] = ;
}
}
//cout<<tot<<endl;
for (int i = ; i <= n; ++i)
{
for (int j = ; j <= m; ++j)
{
int o = read();
if (o) num[id[i][j]]=;
for (int k = ; k <= ; ++ k)
{
int tx=i+dx[k],ty=j+dy[k];
if (tx<n+&&ty<m+) ed[++cnt]=(edge){id[i][j],id[tx][ty],abs(h[i][j]-h[tx][ty])};
}
}
}
// cout<<tot<<endl;
// cout<<n<<endl<<m<<endl;
// cout<<n*m<<endl;
sort(ed + , ed + + cnt);
// cout<<cnt<<endl;
for (register int i = ; i <= cnt ; i ++)
{
int x = ed[i].x, y = ed[i].y;
int fx = Find(x), fy = Find(y);
if (fx== fy) continue;
if (sz[fx] + sz[fy] >= T)
{
if (sz[fx] < T) ans += 1ll * ed[i].val * num[fx];
if (sz[fy] < T) ans += 1ll * ed[i].val * num[fy];
}
if (sz[fx] > sz[fy]) swap(fx, fy);
fa[fx] = fy;
sz[fy] += sz[fx], num[fy] += num[fx];
}
cout<<ans<<endl;
return ;
}

[USACO14JAN]滑雪等级Ski Course Rating的更多相关文章

  1. LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating

    LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating [问题描述] The cross-country skiing course at the winter Mool ...

  2. 【USACO2009 Open】滑雪课程ski

    [USACO2009 Open]滑雪课程 Ski Lessons Time Limit: 1000 ms Memory Limit: 131072 KBytes Description 约翰请贝西去科 ...

  3. [USACO14JAN]Ski Course Rating G

    题目链接:https://www.luogu.com.cn/problem/P3101 Slove 这题我们可以尝试建立一个图. 以相邻的两个点建边,边的权值为两个点高度差的绝对值,然后把边按照边权值 ...

  4. [USACO09OPEN]滑雪课Ski Lessons

    题目描述 Farmer John wants to take Bessie skiing in Colorado. Sadly, Bessie is not really a very good sk ...

  5. BZOJ 1571: [Usaco2009 Open]滑雪课Ski

    Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里,每天会提供S(0<=S& ...

  6. [bzoj1571][Usaco2009 Open]滑雪课Ski

    题目描述 Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里,每天会提供S(0<=S<=100 ...

  7. 【贪心优化dp决策】bzoj1571: [Usaco2009 Open]滑雪课Ski

    还有贪心优化dp决策的操作…… Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里 ...

  8. BZOJ——1571: [Usaco2009 Open]滑雪课Ski

    http://www.lydsy.com/JudgeOnline/problem.php?id=1571 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit:  ...

  9. [USACO2009 OPEN] 滑雪课 Ski Lessons

    洛谷P2948 看到题目就觉得这是动规但一直没想到如何状态转移……看了别人的题解之后才有一些想法 f[i][j]:前i单位时间能力值为j可以滑的最多次数 lessons[i][j]:结束时间为i,获得 ...

随机推荐

  1. apache ignite系列(二):配置

    ignite有两种配置方式,一种是基于XML文件的配置,一种是基于JAVA代码的配置: 这里将ignite常用的配置集中罗列出来了,一般建议使用xml配置. 1,基于XML的配置 <beans ...

  2. Mybatis值ResultMap的使用详解

    Mybatis的定义 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  3. Linux 笔记 - 第十八章 Linux 集群之(一)Keepalived 高可用集群

    一.前言 Linux 集群从功能上可以分为两大类:高可用集群和负载均衡集群.此处只讲高可用集群,负载均衡放在下一篇博客讲解. 高可用集群(High Availability Cluster,简称 HA ...

  4. 重学js之JavaScript 面向对象的程序设计(创建对象)

    注意: 本文章为 <重学js之JavaScript高级程序设计>系列第五章[JavaScript引用类型]. 关于<重学js之JavaScript高级程序设计>是重新回顾js基 ...

  5. 复习0824js

    编程思想: 面向过程:凡事亲力亲为,所有事情的过程都要清楚,注重的是过程. 面向对象:提出需求,找到对象,对象解决这个问题,我们要结果,注重的是结果. 面向对象的特性:封装,继承,多态: JS: 是一 ...

  6. [Optimized Python] 17 - Performance bottle-neck

    前言 对于一门编程语言,没接触到“优化”和“库代码”的源码理解程度,不足以谈“掌握”二字. 本篇的学习笔记,同时也意味着自己终于触及到了Python的junior国际水准.(joke) 要学的东西有很 ...

  7. Android 正 N 边形圆角头像的实现

    卖一下广告,欢迎大家关注我的微信公众号,扫一扫下方二维码或搜索微信号 stormjun94(徐公码字),即可关注. 目前专注于 Android 开发,主要分享 Android开发相关知识和一些相关的优 ...

  8. 设计模式-Builder和Factory模式区别

    Builder和Factory模式区别 Builder模式结构: Factory模式一进一出,Builder模式是分步流水线作业.当你需要做一系列有序的工作或者按照一定的逻辑来完成创建一个对象时 Bu ...

  9. c# 9.0 特性提案 简化空参数验证代码

    简而言之就是将已存在的特性null参数验证,使用一个简单的语义标注进一步简化. 对于如下这段代码 // Before void Insert(string s) { if (s is null) { ...

  10. windows下配置多个tomcat步骤

    步骤如下: 1.使用压缩版的tomcat不能使用安装版的.2.第一个tomcat的配置不变.3.增加环境变量CATALINA_HOME2,值为新的tomcat的地址:增加环境变量CATALINA_BA ...