zoukankan      html  css  js  c++  java
  • HDU 3440 House Man

    House Man

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 3440
    64-bit integer IO format: %I64d      Java class name: Main
     
    In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the shortest house and make N-1 jumps, with each jump taking him to a taller house than the one he is jumping from. When finished, he will have been on every house exactly once, traversing them in increasing order of height, and ending up on the tallest house. 
    The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house. 
    The crazy super man have an ability—move houses. So he is going to move the houses subject to the following constraints:
    1. All houses are to be moved along a one-dimensional path. 
    2. Houses must be moved at integer locations along the path, with no two houses at the same location. 
    3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order. 
    4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter). 
    Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training. 
     

    Input

    In the first line there is an integer T, indicates the number of test cases.(T<=500)
    Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique. 
     

    Output

    For each test case , output “Case %d: “first where d is the case number counted from one, then output a single integer representing the maximum distance between the shortest and tallest house, subject to the constraints above, or -1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
     

    Sample Input

    3
    4 4 
    20 30 10 40 
    5 6 
    20 34 54 10 15 
    4 2 
    10 20 16 13 

    Sample Output

    Case 1: 3
    Case 2: 3
    Case 3: -1

    Source

     
    解题:差分约束。。。注意不能重叠,间距不能大于跳的距离
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 1010;
    18 struct arc{
    19     int to,w,next;
    20     arc(int x = 0,int y = 0,int z = -1){
    21         to = x;
    22         w = y;
    23         next = z;
    24     }
    25 };
    26 struct House{
    27     int h,id;
    28 };
    29 arc e[200000];
    30 House hs[maxn];
    31 int head[maxn],cnt[maxn],d[maxn],tot,N,D;
    32 bool in[maxn];
    33 void add(int u,int v,int w){
    34     e[tot] = arc(v,w,head[u]);
    35     head[u] = tot++;
    36 }
    37 bool cmp(const House &x,const House &y){
    38     return x.h < y.h;
    39 }
    40 bool spfa(int s){
    41     memset(d,0x3f,sizeof(d));
    42     memset(in,false,sizeof(in));
    43     memset(cnt,0,sizeof(cnt));
    44     stack<int>q;
    45     d[s] = 0;
    46     q.push(s);
    47     ++cnt[s];
    48     while(!q.empty()){
    49         int u = q.top();
    50         q.pop();
    51         in[u] = false;
    52         for(int i = head[u]; ~i; i = e[i].next){
    53             if(d[e[i].to] > e[i].w + d[u]){
    54                 d[e[i].to] = e[i].w + d[u];
    55                 if(!in[e[i].to]){
    56                     in[e[i].to] = true;
    57                     if(++cnt[e[i].to] > N) return false;
    58                     q.push(e[i].to);
    59                 }
    60             }
    61         }
    62     }
    63     return true;
    64 }
    65 int main() {
    66     int T,cs = 1;
    67     scanf("%d",&T);
    68     while(T--){
    69         scanf("%d %d",&N,&D);
    70         memset(head,-1,sizeof(head));
    71         tot = 0;
    72         for(int i = 1; i <= N; ++i){
    73             scanf("%d",&hs[i].h);
    74             hs[i].id = i;
    75             if(i < N) add(i+1,i,-1);
    76         }
    77         sort(hs+1,hs+N+1,cmp);
    78         for(int i = 2; i <= N; ++i) add(min(hs[i-1].id,hs[i].id),max(hs[i-1].id,hs[i].id),D);
    79         printf("Case %d: ",cs++);
    80         if(spfa(min(hs[1].id,hs[N].id))){
    81             printf("%d
    ",d[max(hs[1].id,hs[N].id)]);
    82         }else puts("-1");
    83     }
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    改进动物园
    异常动手脑总结
    代码大全2 阅读笔记
    抽象和接口
    Appium+python自动化2-启动百度app
    Appium+python自动化1-环境搭建
    python之图像识别
    python之栈和队列
    python之语音识别(speech模块)
    使用Python计算IP、TCP、UDP校验和
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4062664.html
Copyright © 2011-2022 走看看