LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating

LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating


 

【问题描述】

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)个格子(包括起点本身)。

【输入样例】

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

【输出样例】

24


 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 500 + 5;
const int maxm = maxn * maxn;
long long am[maxm], fa[maxm], is[maxm];
//am amount 集合子点数量 ,fa father 父节点编号 , is is_start 集合内的起点个数 
long long he[maxn][maxn], id[maxn][maxn];
//he height (i,j)高度  
long long n, m, t, cntv, sign, cnte;
//cntv count_vertex,cnte count_edge  
struct edge
{
    long long beg, to, len; // beg起点 , to 终点 ,len 边权 
} a[1000000]; //血泪(必须要开1000000) 
edge mk(long long x,long long  y, long long z)
{
    edge t = (edge)
    {
        x, y, z
    };
    return t;
}//这是一个制作边的函数 
bool cmp(edge a, edge b)
{
    return a.len < b.len;
}//这是一个将边排序的函数(服务于贪心思想) 
long long find(long long x)
{
    if(x == fa[x])return x;
    else return fa[x] = find(fa[x]);
}//并查集标准find函数 
void merge(int x, int y)
{
    if(x < y)swap(x, y);

    fa[y] = x;
    am[x] += am[y];
  //  am[y] = 0;
    is[x] += is[y];
}//这是一个合并函数(维护了两个集合的子点数个数,起点个数) 
int main()
{
    scanf("%lld%lld%lld", &n, &m, &t);

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            scanf("%lld", &he[i][j]);
            cntv++;
            id[i][j] = cntv;
            //给坐标为(i,j)的点编号 cntv
        }
    } 
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            scanf("%lld", &sign);

            if(sign)is[id[i][j]] = 1;//这个点是一个起点 

            if(i < n)
            {
                a[++cnte] = mk(id[i][j], id[i + 1][j], abs(he[i][j] - he[i + 1][j]));
            }

            if(j < m)
            {
                a[++cnte] = mk(id[i][j], id[i][j + 1], abs(he[i][j] - he[i][j + 1]));
            }
            
            //建边 
        }
    }

    sort(a + 1, a + 1 + cnte, cmp);
    
    //贪心 
     
    long long ans = 0;

    for(int i = 1; i <= maxm; i++)
    {
        am[i] = 1; //i是自己的一个集 
        fa[i] = i;
    }
    for(int i = 1; i <= cnte; i++)
    {
        int x = a[i].beg;
        int y = a[i].to;
        int z = a[i].len;
        int fx = find(x);
        int fy = find(y);
        if(fx==fy)continue; 
        
        if(am[fx] + am[fy] >= t)
        {
            if(am[fx] < t)ans += z * is[fx];
            //如果一个集合的数目小于t,则其没有算过边。    
            if(am[fy] < t)ans += z * is[fy];
        }

        merge(fx, fy);
    }

    printf("%lld", ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/yangxuejian/p/10993576.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值