HDU3440(差分约束)
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
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
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
Sample Input
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Sample Output
Case 2: 3
Case 3: -1
Author
Source
将这些房子在一维的方向上重新摆放(但是保持输入时的相对位置不变) , 使得最矮的房子和最高的房子水平距离最大
差分约束系统建图。
//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(差分约束)的更多相关文章
- 【HDU3440】House Man (差分约束)
题目: Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
随机推荐
- spark踩坑——dataframe写入hbase连接异常
最近测试环境基于shc[https://github.com/hortonworks-spark/shc]的hbase-connector总是异常连接不到zookeeper,看下报错日志: 18/06 ...
- zookeeper的安装与配置(单机和集群)
单机模式: 1.首先去官网下载zookeeper的包 zookeeper-3.4.10.tar.gz 2.用FTP上传到服务器或者Linux虚拟机的/usr/local目录下 3.解压文件tar -z ...
- cglib invoke 和 invokeSuper 可用的组合
在深入字节码理解invokeSuper无限循环的原因中,我们理解的cglib的原理和其中一个合理的调用方式.但是这个调用方式是基于类的,对所有实例生效.实际场景中,我们可能只是希望代理某个具体的实例, ...
- Synchronzied(内置锁)
原文地址:深入JVM锁机制1-synchronized 1. 线程的状态与转换 当多个线程同时请求某个对象监视器时,对象监视器会设置几种状态用来区分请求的线程: Contention List:所有请 ...
- 【转】iOS中属性与成员变量的区别
[转载自并整理 http://blog.csdn.net/itianyi/article/details/8618128] 一.类Class中的属性property 在ios第一版中,我们为输出口同时 ...
- IQueryable与IEnumerable
IEnumerable: 从服务器处取回所有数据,在客户端根据过滤条件进行过滤再返回结果. IQueryable: 从服务器处进行过滤,直接返回过滤后的结果.
- odoo开发笔记-自定义发送邮件模板
1. 首先激活开发者模式 2. 点击设置 - Email - 模板 - “选择你需要修改的模板” 我们以销售模块-报价单 邮件模板为例 来说明. quote order 原先默认模板,发出的邮件显示效 ...
- 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks) —— 3.Programming Assignments: Deep Neural Network - Application
Deep Neural Network - Application Congratulations! Welcome to the fourth programming exercise of the ...
- 剑指offer八之跳台阶
一.题目 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 二.思路 a.如果两种跳法,1阶或者2阶,那么假定第一次跳的是一阶,那么剩下的是n-1个台阶,跳法 ...
- eclipse下搭建shell脚本编辑器--安装开发shell的eclipse插件shelled
具体请看: 亲测有效: http://www.cnblogs.com/shellshell/p/6122811.html