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 ≤ ...
随机推荐
- Python开发爆破工具
上一篇讲到了如何用Python开发字典,而当我们手里有了字典 就可以进一步去做爆破的任务了,可以用现成的工具,当然也可以自己写 接下来我就要一步一步来写爆破工具! 爆破MySQL: 想要爆破MySQL ...
- [Swift-2019力扣杯春季初赛]4. 从始点到终点的所有路径
给定有向图的边 edges,以及该图的始点 source 和目标终点 destination,确定从始点 source 出发的所有路径是否最终结束于目标终点 destination,即: 从始点 so ...
- 一个applicationContext 加载错误导致的阻塞解决小结
问题为对接一个sso的验证模块,正确的对接姿势为,接入一个 filter, 然后接入一个 SsoListener . 然而在接入之后,却导致了应用无法正常启动,或者说看起来很奇怪,来看下都遇到什么样的 ...
- Core 接口发布报错
An error occurred while starting the application 提示:启动应用程序时发生错误 这个错误在Startup启动项里面 要找具体的报错位置 如下 这样就可以 ...
- log4j日志输出到日志文件中和控制台中 +log4j配置详解
1.引入log4j的jar包 https://mvnrepository.com/,可以找到log4j的jar和依赖. 2.创建log4j.properties,并配置log4j #设置日志的级别 , ...
- Ubuntu 16.04 下octave的使用入门
SciLab和octave是开源的且免费的矩阵计算工具,二者都有希望成为矩阵计算的新宠.相比之下, octave与MatLab的兼容性更高. octave遵循GPL协议(GNU General Pub ...
- Python 游戏之旅(Pygame)
Pygame是跨平台Python模块,专为电子游戏设计,包含图像.声音.建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚.基于这样一个设想,所有需要的游戏功能和理念 ...
- Saiku多用户使用时数据同步刷新(十七)
Saiku多用户使用时数据同步刷新 这里我们需要了解一下关于saiku的刷新主要有两种数据需要刷新: >1 刷新数据库的表中的数据,得到最新的表数据进行展示. >2 刷新cube信息,得到 ...
- IT老人,给后辈的十一点建议
我已经在IT业打拼9年了,从完全自学成为技术团队leader到PM也确实总结了不少的经验,自己也经常跟学弟学妹聊天,分享职场经验,当老家有人报考计算机或者从事相关工作时也会咨询我的意见,我很明白IT人 ...
- Elastic Job学习
从执行的结果来看,随着服务器的增加,分片的结果也不一样 当只有一台服务器的时候,所有分片都到这一台服务器上 当服务器增加到两台的时候,分片的结果是0 1 2 3 4和5 6 7 8 9 当服务器增加到 ...