House Man

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

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

 
题意:给我们n座房子,房子的高度各不相同, 从最低的房子开始, 每次跳到更高的房子, 跳n-1次最能跳到最高的房子了,但是每次跳跃的距离不能超过d

     将这些房子在一维的方向上重新摆放(但是保持输入时的相对位置不变) , 使得最矮的房子和最高的房子水平距离最大

差分约束系统建图。

 //2017-08-29
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f; int head[N], tot;
struct Edge{
int to, next, w;
}edge[M]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} int n, m, c;
bool vis[N];
int dis[N], cnt[N]; bool spfa(int s, int n){
memset(vis, , sizeof(vis));
memset(dis, INF, sizeof(dis));
memset(cnt, , sizeof(cnt));
vis[s] = ;
dis[s] = ;
cnt[s] = ;
deque<int> dq;
dq.push_back(s);
int sum = , len = ;
while(!dq.empty()){
// LLL 优化
while(dis[dq.front()]*len > sum){
dq.push_back(dq.front());
dq.pop_front();
}
int u = dq.front();
sum -= dis[u];
len--;
dq.pop_front();
vis[u] = ;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].w){
dis[v] = dis[u] + edge[i].w;
if(!vis[v]){
vis[v] = ;
// SLF 优化
if(!dq.empty() && dis[v] < dis[dq.front()])
dq.push_front(v);
else dq.push_back(v);
sum += dis[v];
len++;
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} struct Node{
int id, height;
bool operator<(const Node a) const{
return height < a.height;
}
}house[N]; int main()
{
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
int T, n, d, kase = ;
cin>>T;
while(T--){
init();
cin>>n>>d;
for(int i = ; i < n; i++){
cin>>house[i].height;
house[i].id = i;
}
sort(house, house+n);
//令d[i]表示i到最左边点的距离。
for(int i = ; i < n-; i++){
// d[i+1] - d[i] >= 1 约束1
// d[i] - d[i+1] <= -1
// i+1 -> i : -1
add_edge(i+, i, -);
// d[v] - d[u] <= d | v > u 约束2
if(house[i+].id < house[i].id)
add_edge(house[i+].id, house[i].id, d);
else
add_edge(house[i].id, house[i+].id, d);
}
int s, t;
//有向边都是 标号小的点 ---> 标号大的点(除开边权为-1的边),所以找最短路的时候也要约定从标号小的点到标号大的点
if(house[].id < house[n-].id){
s = house[].id;
t = house[n-].id;
}else{
s = house[n-].id;
t = house[].id;
}
cout<<"Case "<<++kase<<": ";
if(spfa(s, n))
cout<<dis[t]<<endl;
else cout<<-<<endl;
} return ;
}

HDU3440(差分约束)的更多相关文章

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

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

  2. Candies-POJ3159差分约束

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

  3. poj3159 差分约束 spfa

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

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

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

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

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

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

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

  7. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  8. [USACO2005][POJ3169]Layout(差分约束)

    题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...

  9. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

随机推荐

  1. 序列化 pickle shelve json configparser

    模块pickle(皮考) dumps(当破死)序列化. 把对象转化成bytes loads(楼死) 反序列化. 吧bytes转化成对象 dic = {"jay": "周杰 ...

  2. 为ElasticSearch添加HTTP基本认证

    ES的HTTP连接没有提供任何的权限控制措施,一旦部署在公共网络就容易有数据泄露的风险,尤其是加上类似elasticsearch-head这样友好的前端界面,简直让你的数据瞬间裸奔在黑客的眼皮底下.项 ...

  3. django -- 美多订单分表

    订单分表: 随着公司业务增长,如果每天1000多万笔订单的话,3个月将有约10亿的订单量,之前数据库采用单表的形式已经不满足于业务需求,数据库改造迫在眉睫. 解决思路: 按月分表,将原订单表拆分为 o ...

  4. 01-Linux的基本指令

    Linux的基本指令 基础指令(重点) 1.ls指令: 含义:ls(list) 用法1:#ls 含义:列出当前工作目录下的所有文件/文件夹的名称 用法2:#ls  路径 含义:列出指定路径下的所有文件 ...

  5. POJ 2572

    #include<stdio.h> #include<iostream> #include<string> using namespace std; int mai ...

  6. vue教程1-07 模板和过滤器

    vue教程1-07 模板和过滤器 一.模板 {{msg}} 数据更新模板变化 {{*msg}} 数据只绑定一次 {{{msg}}} HTML转意输出 <!DOCTYPE html> < ...

  7. Java异常处理设计(一)

    很多次的经验教训,让我不得不重视异常处理.经常遇到的问题如下:1)日志不准确,错误原因难以查明!!2)日志量太大,查找麻烦!!3)哪里需要记录日志,哪里不用记录日志?往往随心所欲!!分析以上问题,深入 ...

  8. 用AOP思想改造一个服务器的数据存储

    背景是有一个游戏服务器一直以来都是写SQL的, 后来改过一段时间的redis, 用的是别的员工写的类orm方式将实体类型映射成各种key-value对进行写入, 但是仍有一个缺点就是需要在增\删\改的 ...

  9. Hive中的Row_Number()使用

    语法:row_number() over (partition by 字段a order by 计算项b desc ) rank --这里rank是别名 partition by:类似hive的建表, ...

  10. 阿里巴巴Java开发规范---个人总结

    一.编程规约 (一) 命名规约 1. [强制]所有编程相关命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ / ...