Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3044    Accepted Submission(s): 1275

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栋房子,给出每栋房子的高度和开始时的相对位置,可以移动一些房子,但不能改变这些房子的相对位置,现在从最矮的房子开始,每次跳至比它高的第一栋房子, 而且每次跳跃的水平距离最大是D,房子不能在同一位置,只能在整点位置。问最矮的房子和最高的房子之间的最大距离可以是多少?如果不能从最矮的房子到达最高的房子则输出-1.

分析:令d[i]表示第i栋房子与第一栋房子之间的最大距离,那么我们要求的就是的的d[n],求最短路即可,首先每栋房子之间的相对位置已经确定且不能在同一位置,那么d[i+1] > d[i],每次要跳至比它高的房子上,那么我们需要对房子按高度排序。因为开始时已经规定标号小的点在标号大的点的左边,这样,我们如果从标号大的点到标号小的点,建一条这样的边就会有问题,只能按小到大建边,而且如果两个排序后相邻房子之间的标号大于D的话则不可能到最高的房子,因为房子不可能在同一位置,他们之间的距离至少是D。约束条件只有这两者,建边时需要处理一下方向。最后如果最高的房子标号比矮的房子小的话,则以最高的房子为源点进行spfa,如果存在负环则输出-1.

杭电炸了。。。放个std

#include <bits/stdc++.h>
using namespace std; const int N = , M = ;
const int INF = 0x3f3f3f3f; struct house{
int he, id;
bool operator < (const house& x)const { return he < x.he; }
}h[N];
struct edge{
int v, d, next;
edge(int v, int d, int n):v(v), d(d), next(n){}
edge(){}
}ed[M];
int head[N], d[N], vis[N], cnt[N];
int n, s, e, k;
queue<int> q;
void init() {
k = ;
memset(head, -, sizeof(int) * n);
memset(d, INF, sizeof(int) * n);
memset(vis, , sizeof(int) * n);
memset(cnt, , sizeof(int) * n);
for (int i = ; i < n; i++) h[i].id = i;
while (!q.empty()) q.pop();
}
void add(int u, int v, int d) {
ed[k] = edge(v, d, head[u]);
head[u] = k++;
}
int spfa() {
d[s] = ; cnt[s]++;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
vis[x] = ;
for (int i = head[x]; i != -; i = ed[i].next) {
int t = ed[i].v;
if (d[t] > d[x] + ed[i].d) {
d[t] = d[x] + ed[i].d;
if (!vis[t]) {
vis[t] = ; q.push(t);
if (++cnt[t] > n) return -;
}
}
}
}
return d[e];
}
int main() {
int t, ca = ;
scanf("%d", &t);
while (t--) {
int d;
scanf("%d %d", &n, &d);
init();
for (int i = ; i < n; i++) scanf("%d", &h[i].he);
sort(h, h+n);
int flag = ;
for (int i = ; i < n- && flag; i++) {
add(i+, i, -);
int u = min(h[i].id, h[i+].id), v = max(h[i].id, h[i+].id);
if (v - u > d) flag = ;
add(u, v, d);
}
s = min(h[].id, h[n-].id), e = max(h[].id, h[n-].id);
printf("Case %d: %d\n", ++ca, flag ? spfa() : -);
}
return ;
}

HDU3440 House Man的更多相关文章

  1. 【HDU3440】House Man (差分约束)

    题目: Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop ...

  2. HDU3440(差分约束)

    House Man Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU3440 House Man (差分约束)

    In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. To ...

  4. hdu3440 House Man 【差分约束系统】

    House Man Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. D2欧拉路,拓扑排序,和差分约束

    第一题:太鼓达人:BZOJ3033 题意:给出k,求一个最长的M位01串,使其从每一个位置向后走k个得到 的M个k位01串互不相同(最后一个和第一个相邻,即是一个环).输出 字典序最小的答案. 2 ≤ ...

随机推荐

  1. 从字节码看java类型转换【 深入理解 (T[]) new Object[size] 】

    我们都知道,java中对类型的检查是很严格的,所以我们平操作时,也往往很小心. 如题: (T[]) new Object[size],这种写法是一般我们是不会干的!但是有点经验的同学,还是会遇到这样写 ...

  2. 【design patterns】设计模式

    1.单例设计模式(singleton) 用途举例:对于多个程序使用同一个配置信息对象时比如在连接数据库时使用单例模式,每次只取出一个连接 步骤:①私有化该类的构造函数 ②私有化一个静态的对象 ③公有化 ...

  3. Django ListView实现分页

    效果: url.py main-urls from django.urls import path,include urlpatterns = [ path('admin/', admin.site. ...

  4. 【新手向】使用nodejs抓取百度贴吧内容

    参考教程:https://github.com/alsotang/node-lessons 1~5节 1. 通过superagent抓取页面内容 superagent .get('http://www ...

  5. Ocelot简易教程(七)之配置文件数据库存储插件源码解析

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9852711.html 上篇文章给大家分享了如何集成我写的一个Ocelot扩展插件把Ocelot的配置存储 ...

  6. Java垃圾回收(GC)机制详解

    一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分配内存空间而不进行回收.除非内存无限大,我们可以任性的分配而不回收,但是事实并非如此.所以,垃圾回收是必须的. 二. ...

  7. mysql 开发进阶篇系列 16 MySQL Server(myisam key_buffer)

    一.概述 mysql 提供了很多参数来进行服务器的设置,当服务第一次启动的时候,所有启动参数值都是系统默认的.这些参数在很多生产环境下并不能满足实际的应用需求.在这个系列中涉及到了liunx 服务器, ...

  8. sql server 备份与恢复系列二 事务日志概述

    1.1  日志文件与数据文件一致性 在上一章备份与恢复里了解到事务日志的重要性,这篇重点来了解事务日志. 事务日志记录了数据库所有的改变,能恢复该数据库到改变之前的任意状态.在sql server实例 ...

  9. mysql 开发基础系列4 字符数据类型

    字符串类型 1.1 CHAR 和VARCHAR 类型 CHAR 列的长度固定为创建表时声明的长度,VARCHAR 列中的值为可变长字符串.在检索的时候,CHAR 列删除了尾部的空格,而VARCHAR ...

  10. mysql 开发基础系列1 表查询操作

    在安装完数据库后,不管是windows 还是linux平台,  mysql的sql命令都大同小异,相关命令都是相同的,每个命令结束后 都以  ;  结尾, 注意在windows平台中表名是不区分大小写 ...