zoukankan      html  css  js  c++  java
  • 8010: Mountaineers

    贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。

    8010: Mountaineers

    时间限制: 3 Sec  内存限制: 128 MB
    提交: 20  解决: 9
    [提交] [状态] [讨论版] [命题人:admin]

    题目描述

    The Chilean Andes have become increasingly popular as a destination for backpacking and hiking. Many parts of the Andes are quite remote and thus dangerous. Because of this, the Ministry of Tourism wants to help travelers plan their trips. In particular, the travelers need to know how high they will have to climb during their journey, as this information will help them decide which equipment they need to bring. The Ministry has tasked you to provide the aspiring mountaineers with this data.
    You are given a topographic map of a part of the Andes, represented as a two-dimensional grid of height values, as well as the list of origins and destinations. Mountaineers can move from each grid cell to any of the four adjacent cells. For each mountaineer find the minimal height that they must be able to reach in order to complete their journey.

    输入

    The input consists of:
    •one line with three integers m, n and q (1 ≤ m, n ≤ 500, 1 ≤ q ≤ 105), where m is the number of rows, n is the number of columns, and q is the number of mountaineers;
    •m lines, each with n integers h1, ... , hn (1 ≤ hi ≤ 106), the height values in the map;
    •q lines, each with four integers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ m, 1 ≤ y1, y2 ≤ n), describing a mountaineer who wants to trek from (x1, y1) to (x2, y2).
    The top left cell of the grid has coordinates (1, 1) and the bottom right cell has coordinates (m, n).

    输出

    Output q integers, the minimal height for each mountaineer, in the same order as in the input.

    样例输入

    3 5 3
    1 3 2 1 3
    2 4 5 4 4
    2 1 3 2 2
    1 1 3 2
    2 4 2 2
    1 4 3 4
    

    样例输出

    2
    4
    3
    

    来源/分类

     
    思路:
    建树+lca。
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=3e5+10,inf=0x3f3f3f3f;
    int n,m,q;
    int p[maxn],anc[maxn][20],v[maxn],dep[maxn],id[maxn];
    bool vis[maxn];
    vector<int>e[maxn];
    inline int rt(int x,int y) {
        return x*m+y;
    }
    int fnd(int x){
        return p[x]==x?x:p[x]=fnd(p[x]);
    }
    void dfs(int x){
        for (int i=1; ~anc[x][i-1]&& ~anc[anc[x][i-1]][i-1];++i)
            anc[x][i]=anc[anc[x][i-1]][i-1];
        for (auto y:e[x]){
            anc[y][0]=x;
            dep[y]=dep[x]+1;
            dfs(y);
        }
    }
    int lca(int x,int y){
        if(dep[x]>dep[y]) swap(x,y);
        for (int i=19; i>=0; --i){
            if(~anc[y][i] && dep[x]<=dep[anc[y][i]])
                y=anc[y][i];
        }
        if(x==y) return x;
        for (int i=19; i>=0; --i){
            if(anc[x][i]!=anc[y][i]){
                x=anc[x][i];
                y=anc[y][i];
            }
        }
        return anc[x][0];
    }
    int x,y,a,b;
    int main(){
    //freopen("in.txt","r",stdin);
        memset(anc,-1,sizeof(anc));
        scanf("%d%d%d",&n,&m,&q);
        for (int i=0; i<n; i++){
            for (int j=0; j<m; j++){
                scanf("%d",&v[rt(i,j)]);
                id[rt(i,j)]=rt(i,j);
            }
        }
        sort(id,id+n*m,[](int x,int y){return v[x]<v[y];});
        for (int i=0; i<n*m; ++i){
            int pos=id[i];
            p[pos]=pos;
            vis[pos]=1;
            if(pos%m && vis[pos-1] && fnd(pos-1)!=pos){
                e[pos].push_back(fnd(pos-1));
                p[fnd(pos-1)]=pos;
            }
            if((pos+1)%m && vis[pos+1] && fnd(pos+1)!=pos){
                e[pos].push_back(fnd(pos+1));
                p[fnd(pos+1)]=pos;
            }
            if(pos+m<n*m && vis[pos+m] && fnd(pos+m) != pos){
                e[pos].push_back(fnd(pos+m));
                p[fnd(pos+m)]=pos;
            }
            if(pos-m>=0 && vis[pos-m] && fnd(pos-m) != pos){
                e[pos].push_back(fnd(pos-m));
                p[fnd(pos-m)]=pos;
            }
        }
        dfs(fnd(0));
        while(q--){
            scanf("%d%d%d%d",&x,&y,&a,&b);
            x--,y--,a--,b--;
            x=rt(x,y),a=rt(a,b);
            printf("%d
    ",v[lca(x,a)]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    对其他组的评论与意见
    第一阶段结果展示
    团队第一阶段冲刺绩效评估
    团队冲刺第八天站立会议
    团队检查博客
    团队冲刺地七天站立会议
    团队冲刺第六天站立会议
    团队冲刺第五天战略站立会议
    团队冲刺第四天战略会议站立
    No2_2.接口继承多态_Java学习笔记_继承
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9638114.html
Copyright © 2011-2022 走看看