zoukankan      html  css  js  c++  java
  • 洛谷1514 引水入城

    {好遗憾啊,这个题只能过五个点,三个WA,两个TLE,不知道为什么……望大神赐教}

    题目描述

    在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠。该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度。

    为了使居民们都尽可能饮用到清澈的湖水,现在要在某些城市建造水利设施。水利设施有两种,分别为蓄水厂和输水站。蓄水厂的功能是利用水泵将湖泊中的水抽取到所在城市的蓄水池中。
    因此,只有与湖泊毗邻的第1 行的城市可以建造蓄水厂。而输水站的功能则是通过输水管线利用高度落差,将湖水从高处向低处输送。故一座城市能建造输水站的前提,是存在比它海拔更高且拥有公共边的相邻城市,已经建有水利设施。由于第N 行的城市靠近沙漠,是该国的干旱区,所以要求其中的每座城市都建有水利设施。那么,这个要求能否满足呢?如果能,请计算最少建造几个蓄水厂;如果不能,求干旱区中不可能建有水利设施的城市数目。

    输入输出格式

    输入格式:

    输入文件的每行中两个数之间用一个空格隔开。输入的第一行是两个正整数N 和M,表示矩形的规模。接下来N 行,每行M 个正整数,依次代表每座城市的海拔高度。

    输出格式:

    输出有两行。如果能满足要求,输出的第一行是整数1,第二行是一个整数,代表最少建造几个蓄水厂;如果不能满足要求,输出的第一行是整数0,第二行是一个整数,代表有几座干旱区中的城市不可能建有水利设施。

    输入输出样例

    输入样例#1:

    【输入样例1】
    2 5
    9 1 5 4 3
    8 7 6 1 2
    
    【输入样例2】
    3 6
    8 4 5 6 4 4
    7 3 4 3 3 3
    3 2 2 1 1 2

    输出样例#1:

    【输出样例1】
    1
    1
    
    【输出样例2】
    1
    3

    说明

    【样例1 说明】
    只需要在海拔为9 的那座城市中建造蓄水厂,即可满足要求。
    【样例2 说明】

    上图中,在3 个粗线框出的城市中建造蓄水厂,可以满足要求。以这3 个蓄水厂为源头
    在干旱区中建造的输水站分别用3 种颜色标出。当然,建造方法可能不唯一。
    【数据范围】

    解题思路

    标准的解题思路:大神们给的都是DFS+BFS+DP,然而我并不会写

    我自己的解题思路:记忆化的DFS,最靠近河边的点对应的线段,利用线段覆盖的动规(f[i]:=min(f[i],w[line[l]-1]+1);)

     1 program Water;
     2 uses math;
     3 type dott=record
     4      l,r:Longint;
     5      end;
     6 const fx:array[1..4] of longint=(1,0,0,-1);
     7        fy:array[1..4] of longint=(0,-1,1,0);
     8 var dot:array[0..500,0..500] of dott;
     9     f:Array[0..500,0..500] of longint;
    10     pd:array[0..500] of boolean;
    11     ans,i,j,n,m,r1:longint;
    12     w:array[0..500] of longint;
    13 procedure sort(l,r: longint);
    14       var
    15          i,j,x,y: longint;
    16       begin
    17          i:=l;
    18          j:=r;
    19          x:=dot[1,(l+r) div 2].l;
    20          y:=dot[1,(l+r) div 2].r;
    21          repeat
    22            while (dot[1,i].l<x) or ((dot[1,i].l=x) and (dot[1,i].r>y)) do
    23             inc(i);
    24            while (x<dot[1,j].l) or ((dot[1,j].l=x) and (dot[1,j].r<y)) do
    25             dec(j);
    26            if not(i>j) then
    27              begin
    28                 dot[1,0]:=dot[1,i];
    29                 dot[1,i]:=dot[1,j];
    30                 dot[1,j]:=dot[1,0];
    31                 inc(i);
    32                 j:=j-1;
    33              end;
    34          until i>j;
    35          if l<j then
    36            sort(l,j);
    37          if i<r then
    38            sort(i,r);
    39       end;
    40 
    41 procedure dfs(j,x,y:Longint);
    42 var  i:longint;
    43 begin
    44     if x=n then
    45     begin
    46         dot[x,y].l:=y;
    47         dot[x,y].r:=y;
    48         pd[y]:=true;
    49         //if y=m then exit;
    50     end;
    51     for i:=1 to 4 do
    52     if f[x+fx[i],y+fy[i]]<f[x,y] then
    53     begin
    54         if dot[x+fx[i],y+
    55         fy[i]].r=-1 then dfs(j,x+fx[i],y+fy[i]);
    56         if dot[x+fx[i],y+fy[i]].r<>-1 then
    57         begin
    58             if dot[x+fx[i],y+fy[i]].l<dot[1,j].l then dot[1,j].l:=dot[x+fx[i],y+fy[i]].l;
    59             if dot[x+fx[i],y+fy[i]].r>dot[1,j].r then dot[1,j].r:=dot[x+fx[i],y+fy[i]].r;
    60         end;
    61     end;
    62 
    63 end;
    64 begin
    65     filldword(f,sizeof(f) div 4,maxlongint div 2);
    66     filldword(w,sizeof(w) div 4,maxlongint div 2);
    67 
    68     read(n,m);
    69     for i:=1 to n do
    70        for j:=1 to m do
    71        begin
    72            dot[i,j].l:=maxlongint div 2;
    73            dot[i,j].r:=-1;
    74        end;
    75     for i:=1 to n do
    76         for j:=1 to m do read(f[i,j]);
    77     for j:=1 to m do dfs(j,1,j);
    78 
    79     for i:=1 to m do  if not pd[i] then inc(ans);
    80     if ans<>0 then
    81     begin
    82         writeln('0');
    83         writeln(ans);
    84         halt;
    85     end;
    86     sort(1,m);
    87     w[0]:=0;
    88     for i:=1 to m do
    89        for j:=1 to m do
    90        if (dot[1,i].l<=j) and (dot[1,i].r>=j) then
    91        w[j]:=min(w[j],w[dot[1,i].l-1]+1);
    92     writeln('1');
    93     writeln(w[m]);
    94 end.
  • 相关阅读:
    Git学习笔记(二)
    Git学习笔记(一)
    在Android中,px,dp,dip,sp的不同之处
    一些好的技术类博客和学习网站(持续更新中)
    nginx参数的详细说明
    Java中的多线程Demo
    Cygwin在线安装指南
    Java项目中的一些注意事项
    tomcat的常用配置
    Java数组初始化
  • 原文地址:https://www.cnblogs.com/wuminyan/p/4737799.html
Copyright © 2011-2022 走看看