House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2306    Accepted Submission(s): 931
Problem Description
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
 
Author
jyd
 
Source
 
Recommend
We have carefully selected several similar problems for you:  3439 3433 3442 3438 3437 

题意:一个男人在n个房子之间跳,他只可以从低的房子跳到高的房子上,
现在给出房子的个数n,男人跳的有多远,每一个房子的高度,男人不能
改变房子的位置,只能按照房子的输入顺序,问男人能否跳n-1次经过所有的
房子到达最高的房子上,如果可以的话输出最大的距离值,否则输出-1
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
#define MAXN 1010
#define MAXM 20000
int head[MAXN],vis[MAXN],dis[MAXN],used[MAXN];
int Instack[MAXN];
int D,n,s,e,cnt;
struct node
{
int u,v;
int val,next;
}edge[MAXM];
struct house
{
int height,pos;
}h[MAXN];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
int cmp(house s1,house s2)
{
return s1.height<s2.height;
}
void getmap()
{
for(int i=1;i<=n;i++)
cin>>h[i].height,h[i].pos=i;
sort(h+1,h+n+1,cmp);
s=min(h[1].pos,h[n].pos);
e=max(h[1].pos,h[n].pos);
for(int i=1;i<=n-1;i++)
{
if(h[i].pos>h[i+1].pos)
add(h[i+1].pos,h[i].pos,D);
else
add(h[i].pos,h[i+1].pos,D);
add(i+1,i,-1);
}
}
void SPFA()
{
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
memset(dis,INF,sizeof(dis));
memset(Instack,0,sizeof(Instack));
used[s]++;
vis[s]=1;
dis[s]=0;
int top=0;
Instack[top++]=s;
while(top)
{
int u=Instack[--top];
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[u]+E.val)
{
dis[E.v]=dis[E.u]+E.val;
if(!vis[E.v])
{
vis[E.v]=1;
used[E.v]++;
if(used[E.v]>n)
{
cout<<-1<<endl;
return ;
}
Instack[top++]=E.v;
}
}
}
}
cout<<dis[e]<<endl;
}
int main()
{
int t;
int k=1;
// std::cout.sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n>>D;
init();
getmap();
cout<<"Case "<<k++<<": ";
SPFA();
}
return 0;
}

hdoj--3440--House Man(差分约束)的更多相关文章

  1. HDOJ 1534 Schedule Problem 差分约束

    差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...

  2. HDOJ 1384 差分约束

    结题报告合集请戳:http://972169909-qq-com.iteye.com/blog/1185527 /*题意:求符合题意的最小集合的元素个数 题目要求的是求的最短路, 则对于 不等式 f( ...

  3. 图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement

    Problem Description Ali has taken the Computer Organization and Architecture course this term. He le ...

  4. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  5. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  6. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  7. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  8. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  9. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  10. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

随机推荐

  1. VUE 基础语法

    <script> //构造器 new Vue({ el: "#apps", data: { MSG: 'THIS IS A TEST Pages', h2test: ' ...

  2. html中<frameset>标签,框架结构各窗口的父级菜单子级菜单关系

    这个问题搞得我头大,并且在查过百度后各位大佬给出的解释我都不能理解,应该是我太白的原因,希望我写的能好理解. 下面文章窗口1.2.3,在代码里分别为chuangkou.chuangkou1.chuan ...

  3. Android线性渐变

    布局实现: 1. 在res中建立drawable文件夹. 2. 在drawable文件夹中建立shape.xml. 3. shape.xml的代码如下: <?xml version=" ...

  4. [原创]C++带空格字符串的输入问题

    字符串一直是一个重点加难点,很多笔试面试都会涉及,带空格的字符串更是十分常见,现在对字符串的输入问题进行一下总结. C++用cin输入的时候会忽略空格以后的字符,比如 char a[100]; cin ...

  5. [原创]Eclipse 安卓开发几个异常的处理办法

    一.代码没有问题,就是报错,重启一下就会好.可以先clean再build; 二.R.Java丢失 网上讲了若干方法,有用android toos的,有clean再build的,我的解决办法是勾选bui ...

  6. RawURL

    Request.RawUrl表示当前页面, Response.Redirect重新打开页面. 意思就是重新打开当前页面. 和下面一样的 string url=Request.RawUrl: Respo ...

  7. SVN客户端安装 Linux

    1.下载 [maintain@HM16-213 software]$ wget http://subversion.tigris.org/downloads/subversion-deps-1.6.1 ...

  8. Java代码运用及算法思路养成——用*号输出形状

    简单的了解了一些循环算法后,尝试用循环算法,输出形状图形 例1矩形与平行四边形的比较(可以看做矩形的每一行在输出前都输出了矩形长度数量-1的空格数量并且依次递减) 例2三角形(三角形可看做半个矩形,考 ...

  9. Nginx的安装与升级

    1,构建Nginx服务器; 2.升级版本; 一, 构建Nginx服务器 1.使用源码包安装nginx软件包 # yum -y install gcc pcre-devel openssl-devel  ...

  10. Ubuntu Server 与 Ubuntu Desktop区别

    今天有位朋友问我,Ubuntu Server 与 Ubuntu Desktop的区别在哪里!区别如下: SERVER没有GUI SERVER没有一堆的桌面软件 SERVER在编译时使用的参数不一样,会 ...