HDOJ题目3440 House Man(差分约束)
House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2256 Accepted Submission(s): 896
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.
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.
-1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
3
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Case 1: 3
Case 2: 3
Case 3: -1
pid=3437" target="_blank">3437
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<stdlib.h>
#define INF 1<<30
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b? a:b)
using namespace std;
struct s
{
int id,num;
}b[1010];
int n,m,vis[1010],head[1010],cnt,dis[1010],out[1010];
int cmp(const void *a,const void *b)
{
return (*(struct s *)a).num-(*(struct s *)b).num;
}
struct node
{
int u,v,w,next;
}edge[2020];
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int spfa(int s)
{
int i;
for(i=1;i<=n;i++)
dis[i]=INF;
memset(vis,0,sizeof(vis));
vis[s]=1;
dis[s]=0;
queue<int>q;
q.push(s);
memset(out,0,sizeof(out));
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
out[u]++;
if(out[u]>n)
return 0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return 1;
}
int main()
{
int t,c=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int i,j;
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<=n;i++)
{
scanf("%d",&b[i].num);
b[i].id=i;
}
qsort(b+1,n,sizeof(b[1]),cmp);
for(i=2;i<=n;i++)
{
add(i,i-1,-1);
if(b[i].id<b[i-1].id)
{
add(b[i].id,b[i-1].id,m);
// add(i,i-1,-1);
}
else
{
add(b[i-1].id,b[i].id,m);
// add(i-1,i,-1);
}
}
// for(i=1;i<=n;i++)
// {
// add(n+1,i,0);
// }
int s=min(b[1].id,b[n].id);
int t=max(b[1].id,b[n].id);
int ans=spfa(s);
printf("Case %d: ",++c);
if(ans)
{
printf("%d\n",dis[t]);
}
else
printf("-1\n");
}
}
HDOJ题目3440 House Man(差分约束)的更多相关文章
- POJ1201 Intervals 【差分约束】
题目链接 POJ1201 题解 差分约束 令\(a[i]\)表示是否选择\(i\),\(s[i]\)表示\(a[i]\)的前缀和 对\(s[i] \quad i \in [-1,50000]\)分别建 ...
- HDOJ 1384 差分约束
结题报告合集请戳:http://972169909-qq-com.iteye.com/blog/1185527 /*题意:求符合题意的最小集合的元素个数 题目要求的是求的最短路, 则对于 不等式 f( ...
- 图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement
Problem Description Ali has taken the Computer Organization and Architecture course this term. He le ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- Candies---hdu3159(spfa+差分约束)
题目链接:http://poj.org/problem?id=3159 题意:有n个小孩,m个关系格式是A B C 表示小孩 B 的糖果数最多比小孩A多C个,相当于B-A<=C; 有m个这样的关 ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- poj3159 最短路(差分约束)
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...
随机推荐
- Android 关于android.os.Build介绍
关于Build类的介绍 这个类为一个获取设备一些初始化信息的类,该类的主要信息都是通过一些static的字段获得: public static final String BOARD The name ...
- 【BZOJ2149】拆迁队(斜率优化DP+CDQ分治)
题目: 一个斜率优化+CDQ好题 BZOJ2149 分析: 先吐槽一下题意:保留房子反而要给赔偿金是什么鬼哦-- 第一问是一个经典问题.直接求原序列的最长上升子序列是错误的.比如\(\{1,2,2,3 ...
- 百度之星2017初赛B1006 小小粉丝度度熊
思路: 考虑到补签卡一定是连续放置才更优,所以直接根据起始位置枚举.预先处理区间之间的gap的前缀和,在枚举过程中二分即可.复杂度O(nlog(n)). 实现: #include <iostre ...
- Android 控制硬加速 hardwareAccelerated
从Android3.0 (API level11)开始,Android的2D显示管道被被设计得更加支持硬加速了.硬加速使用GPU承担了所有在View的canvas上执行的绘制操作. 启用硬加速最简单的 ...
- Leetcode0006--ZigZag Conversion
[转载请注明]https://www.cnblogs.com/igoslly/p/9017638.html 来看一下题目: The string "PAYPALISHIRING" ...
- JS——祝愿墙
注意事项: 1.for循环的下一层注册了事件的话,事件函数中关于变量i的节点元素是不允许出现的,因为在函数加载的时候,只会加载函数名,不会加载函数体,外层for循环会走完一边,变量i一直会停留在最后一 ...
- c++ 以二进制和以文本方式读写文件的区别
在c++项目开发中,时常涉及到文件读写操作.因此在这里先简单梳理和回顾一下文本模式和二进制模式在进行文件读写上的区别. 1.linux平台下文本文件和二进制文件的读写 在linux平台下进行文件读写时 ...
- Python语言之变量1(数值,字符串,布尔)
1.数值 整数:2, -2 长整数:2**1024, 2**2048(真的可以很~长~~~,手残算了个2**100000,IDLE还真给打出来了,ORZ) 浮点数:7.05, 1E2(100.0), ...
- Codeforces_738B
B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Java中接口与接口和类之间的关系
接口和接口之间的关系 继承关系 可以多继承,并且可以多层继承 注意: 1.如果多个父接口中有同名的抽象方法,那么子接口只需要实现一次即可 2.如果多个父接口中有同名的默认方法,那么子接口必须重写默认方 ...