HDU3440 House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3044 Accepted Submission(s): 1275
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.
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.
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Case 2: 3
Case 3: -1
题意:有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的更多相关文章
- 【HDU3440】House Man (差分约束)
题目: Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop ...
- HDU3440(差分约束)
House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU3440 House Man (差分约束)
In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. To ...
- hdu3440 House Man 【差分约束系统】
House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- D2欧拉路,拓扑排序,和差分约束
第一题:太鼓达人:BZOJ3033 题意:给出k,求一个最长的M位01串,使其从每一个位置向后走k个得到 的M个k位01串互不相同(最后一个和第一个相邻,即是一个环).输出 字典序最小的答案. 2 ≤ ...
随机推荐
- CoCos2dx开发:中文乱码
一.FontToUTF8()方法修改字体: 1.HelloWorldScene.h头文件中声明方法: char* FontToUTF8(const char* font); 2.HelloWorldS ...
- centos上ftp服务器的简易安装部署
申明:本示例为centos7 开启ftp服务命令为:systemctl start vsftpd 关闭防火墙命令为systemctl stop firewalld 7版本以下开启ftp服务器为 ser ...
- psnr的定义和python实现
psnr是“Peak Signal to Noise Ratio”的缩写,即峰值信噪比,是一种评价图像的客观标准,它具有局限性,一般是用于最大值信号和背景噪音之间的一个工程项目. peak的中文意思是 ...
- WHERE 子句用于规定选择的标准
WHERE 子句 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句. (也称条件查询语句) 语法SELECT 列名称 FROM 表名称 WHERE 列 运算符 值 下面的& ...
- HBase——HMaster启动之一(HMaster的构建)
首先,让我们来到HMaster的main方法.我们今天的流程就从这里开始. 我们需要注意,下图所示的tool的类型就是HMasterCommandLine. 接下来,让我们来到HMasterComma ...
- Ocelot简易教程(二)之快速开始1
Ocelot简易教程目录 Ocelot简易教程(一)之Ocelot是什么 Ocelot简易教程(二)之快速开始1 Ocelot简易教程(二)之快速开始2 Ocelot简易教程(三)之主要特性及路由详解 ...
- mysql 导入 CSV文件命令行 ERROR 13 (HY000): Can't get stat of
一定要查看好CSV字段结构是否和文件的表结构字段一致 load data local infile 'F:/MySqlData/test1.csv' --CSV文件存放路径 into table st ...
- 远程计算机或设备将不接受连接(电脑能连接网络、QQ能登陆、浏览器无法使用)
第一种方法 1.win+r 2.输入regedit,打开注册表 3.查找Internet Settings(在HKEY_CURRENT_USER\Software\Microsoft\Windows\ ...
- skywalking部署
官方文档:Setup java agent Backend and UI 下载地址:http://skywalking.apache.org/downloads/ 解压后目录 部署UI和收集器 进入w ...
- 10 Tensorflow模型保存与读取
我们的模型训练出来想给别人用,或者是我今天训练不完,明天想接着训练,怎么办?这就需要模型的保存与读取.看代码: import tensorflow as tf import numpy as np i ...