House Man

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

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个房子,线性一维排列,房子有高度,房子不能在同一个位置,现在有一个超人,从最低的房子开始跳,
每次跳的房子的高度要求比上一个房子的高度高,每个房子都要跳,最后要停在最高的房子上
你可以随意改变任意房子之间的距离,但是不能改变房子的相对位置
每次超人最多跳D远,问你超人跳完之后
最低的房子和最高的房子之间的距离最大可以是多少?
分析:
每次要求跳的高度递增且要是最高的房子结束
说明跳的房子的顺序已经排列好了
给线性一维上的房子编个号,1到n
按照高度升序排序,得到的编号顺序就是跳的顺序
假设x[i]:表示房子i的位置
x[i]-x[j]表示房子i和j之间的距离,i>j
比如样例1
高度:20 30 10 40
编号:1  2  3  4
按照高度升序排序之后,跳的顺序为:3 1 2 4
对每次跳:
x[3]-x[1]<=d
x[2]-x[1]<=d
x[4]-x[2]<=d
注意每次都是编号大的房子位置减去编号小的房子位置,这样才能得到两者间的距离
隐藏关系:同一个位置不能有两个房子
则:x[i]-x[i-1]>=1,i属于2到n
变形一下:x[i-1]-x[i]<=-1
现在所有的约束关系是都是这个形式:x[i]-x[j]<=x
开始建图,j->i 权值为x
比如上面的样例:
1->3 权值4
11->2 权值4
2->4 权值4
2->1 权值-1
3->2 权值-1
题目要求是从最低的房子到最高的房子的最大距离
注意:是最高房子和最低房子中编号小的房子出发,做起点
最高房子和最低房子中编号大的房子结束,做终点
因为高的房子编号不一定大,低的房子编号不一定小
然后因为表达式是x[i]-x[j]<=x
所以是求最短路,不能使用dj,因为存在负权
推荐使用spfa
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 9999999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 1005
struct node
{
int v;
LL w;
node(int vv=,LL ww=):v(vv),w(ww) {}
};
struct node1
{
int id,v;
}p[max_v];
bool cmp(node1 a,node1 b)
{
return a.v<b.v;
}
LL dis[max_v];
int vis[max_v];
int cnt[max_v];
vector<node> G[max_v];
queue<int> q; void init()
{
for(int i=; i<max_v; i++)
{
G[i].clear();
dis[i]=INF;
vis[i]=;
cnt[i]=;
}
while(!q.empty())
q.pop();
} int spfa(int s,int n)
{
vis[s]=;
dis[s]=;
q.push(s);
cnt[s]++; while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=; for(int j=; j<G[u].size(); j++)
{
int v=G[u][j].v;
LL w=G[u][j].w; if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(vis[v]==)
{
q.push(v);
cnt[v]++;
vis[v]=; if(cnt[v]>n)
return ;
}
}
}
}
return ;
}
int f(int u,int v)
{
for(int j=; j<G[u].size(); j++)
{
if(G[u][j].v==v)
return ;
}
return ;
}
int main()
{
int t,ca=;
scanf("%d",&t);
while(t--)
{
int n,d;
scanf("%d %d",&n,&d);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i].v);
p[i].id=i;
}
sort(p+,p++n,cmp);
init();
int x,y;
for(int i=;i<=n-;i++)
{
x=p[i].id;
y=p[i+].id;
int u=max(x,y);
int v=min(x,y);
if(f(v,u))
G[v].push_back(node(u,d));
}
for(int i=;i<=n;i++)
{
if(f(i,i-))
G[i].push_back(node(i-,-));
}
int s=min(p[n].id,p[].id);//!!!高的房子不一定编号大,低的房子不一定编号小,因为x[i]-x[j]<=x,要求i>j 请注意
int e=max(p[n].id,p[].id);
int flag=spfa(s,n);
printf("Case %d: ",ca++);
if(flag==)
{
printf("-1\n");
}else
{
printf("%lld\n",dis[e]);
}
}
return ;
} /*
题目意思:
n个房子,线性一维排列,房子有高度,房子不能在同一个位置,现在有一个超人,从最低的房子开始跳,
每次跳的房子的高度要求比上一个房子的高度高,每个房子都要跳,最后要停在最高的房子上
你可以随意改变任意房子之间的距离,但是不能改变房子的相对位置
每次超人最多跳D远,问你超人跳完之后
最低的房子和最高的房子之间的距离最大可以是多少? 分析:
每次要求跳的高度递增且要是最高的房子结束
说明跳的房子的顺序已经排列好了
给线性一维上的房子编个号,1到n
按照高度升序排序,得到的编号顺序就是跳的顺序 假设x[i]:表示房子i的位置
x[i]-x[j]表示房子i和j之间的距离,i>j
比如样例1
高度:20 30 10 40
编号:1 2 3 4
按照高度升序排序之后,跳的顺序为:3 1 2 4
对每次跳:
x[3]-x[1]<=d
x[2]-x[1]<=d
x[4]-x[2]<=d
注意每次都是编号大的房子位置减去编号小的房子位置,这样才能得到两者间的距离 隐藏关系:同一个位置不能有两个房子
则:x[i]-x[i-1]>=1,i属于2到n
变形一下:x[i-1]-x[i]<=-1
现在所有的约束关系是都是这个形式:x[i]-x[j]<=x
开始建图,j->i 权值为x
比如上面的样例:
1->3 权值4
11->2 权值4
2->4 权值4
2->1 权值-1
3->2 权值-1
题目要求是从最低的房子到最高的房子的最大距离
注意:是最高房子和最低房子中编号小的房子出发,做起点
最高房子和最低房子中编号大的房子结束,做终点
因为高的房子编号不一定大,低的房子编号不一定小
然后因为表达式是x[i]-x[j]<=x
所以是求最短路,不能使用dj,因为存在负权
推荐使用spfa */

HDU 3440 House Man(编号排序+线性差分约束跑最短路)的更多相关文章

  1. 【Nowcoder71E】组一组(差分约束,最短路)

    [Nowcoder71E]组一组(差分约束,最短路) 题面 Nowcoder 题解 看到二进制显然就直接拆位,那么区间的按位或和按位与转成前缀和之后,可以写成两个前缀和的值的差的大小关系,那么直接差分 ...

  2. 【拓扑排序或差分约束】Guess UVALive - 4255

    题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...

  3. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  4. HDU 3592 World Exhibition(线性差分约束,spfa跑最短路+判断负环)

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

  5. HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

    题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...

  6. hdu 4598 Difference(奇圈判定+差分约束)

    这是通化邀请赛的题,当时比赛的时候还完全没想法呢,看来这几个月的训练还是有效果的... 题意要求(1) |ai| < T for all i   (2) (vi, vj) in E <=& ...

  7. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  8. poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15349   Accepted: 7379 Descripti ...

  9. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

随机推荐

  1. 5月23日——谈谈对BFC规范的理解

    一.什么是BFC? BFC(block  formatting context):简单来说它就是一种属性,这种属性会影响元素与元素之间的位置.间距 二.形成BFC的条件 1.float:给元素添加浮动 ...

  2. 使用PHP把图片上传到七牛

    先从官网下载SDK,然后新建一个文件,里面包括上传,下载,删除 <?php header("Content-Type:text/html; charset=utf8"); r ...

  3. javascript原型对象与原型链

    在javascript中,当系统加载构造函授后 ,会自动在内存中增加一个对象,这个对象就是原型对象.构造函数和原型对象在内存中表现为相互独立,但两者之间还存在联系,构造函数的prototype是原型对 ...

  4. JS中冒泡排序,选择排序,快速排序

        var arr = [1,4,2,9,7,6,5,4,7,5];     // 冒泡排序(通俗的说就是j 和 j+1打,谁赢了谁去后面)       for(var i = 1;i<ar ...

  5. linux根目录下的文件夹及文件

    bin->放置执行文件的目录,但毕竟特殊,其下放置的是在单用户维护模式下还能被操作的命令.主要有cat.chmod.date. mv.mkdir.cp.bash等常用命令.dev->存放设 ...

  6. Vue -- vue-cli webpack打包开启Gzip 报错

    前两天项目上线,用vue-cli npm run build命令打包,打包完成后我擦吓了一跳,15M.本来暂时不打算优化的,但是每次看着部署包这么大,想想还是先优化一下,让包好看点,免得以后出现心理阴 ...

  7. python websocket client 使用

    import websocket ws = websocket.WebSocket() ws.connect("xx.xx.xx") ws.send("string&qu ...

  8. LeetCode题解之Maximum Depth of N-ary Tree

    1.题目描述 2.问题分析 利用递归fangf 3.代码 int maxDepth(Node* root) { int res = maxdep(root); return res; } int ma ...

  9. Chained Declustering

          此论文描述了在无共享架构的多处理器机器上的数据库系统的数据冗余分布方法.该方法提高了系统的可用性,同时在单节点故障的情况下,可以很好的实现负载均衡.以下是论文的一些摘要,详细可以参见论文原 ...

  10. LevelDB源码分析--Cache及Get查找流程

    本打算接下来分析version相关的概念,但是在准备的过程中看到了VersionSet的table_cache_这个变量才想起还有这样一个模块尚未分析,经过权衡觉得leveldb的version相对C ...