【HDU3440】House Man (差分约束)

题目:

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

题意:

  有n个屋子,超人从最矮的屋子开始,依次跳下比当前屋子高且最接近当前高度的屋子(即按照屋子高度增序来跳),但超人跳跃还有一个水平距离限制D,他每次跳的水平距离<=D。现在给你每个屋子的高度是它们的相对位置,你不能改变屋子的相对位置,但是可以水平移动屋子,使得最矮的屋子和最高的屋子的水平距离最大。如果无论怎样移动,超人都无法跳到最后那个屋子则输出-1。

分析:

  还是差分约束,而且这题很好推,排个序然后找不等关系就好了。那么主要说几个坑点:

  1、一开始我是用二分的,然而TLE,后来发现固定了某个点的值之后用最短路解差分约束,得到的点的解都是它所有取值的可能性中数值最大的。所以我们可以固定起点或者终点的值(哪个在左边就固定哪个),然后用一次差分约束就能求解了。

  2、当n=1时,要特判答案为零。(很坑啊~~)

  3、为什么每次数组开小了都说我TLE...搞到我一直再找哪里打错了导致死循环。判我RE不就好了嘛TAT~~

代码如下:

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 using namespace std;
  7 #include<queue>
  8 #define Maxn 1100
  9 #define Maxm 1000010
 10 
 11 struct node
 12 {
 13     int x,y,c,next;
 14 }t[4*Maxn];int len;
 15 
 16 struct hp
 17 {
 18     int x,id;
 19 }a[Maxn];
 20 
 21 int first[Maxn],dis[Maxn],cnt[Maxn];
 22 int n;
 23 bool inq[Maxn];
 24 
 25 bool cmp(hp x,hp y) {return x.x<y.x;}
 26 int myabs(int x) {return x<0?-x:x;}
 27 
 28 void ins(int x,int y,int c)
 29 {
 30     t[++len].x=x;t[len].y=y;t[len].c=c;
 31     t[len].next=first[x];first[x]=len;
 32 }
 33 
 34 queue<int > q;
 35 bool spfa(int s)
 36 {
 37     while(!q.empty()) q.pop();
 38     memset(dis,63,sizeof(dis));
 39     memset(cnt,0,sizeof(cnt));
 40     memset(inq,0,sizeof(inq));
 41     q.push(s);inq[s]=1;dis[s]=0;
 42     while(!q.empty())
 43     {
 44         int x=q.front();q.pop();inq[x]=0;
 45         for(int i=first[x];i;i=t[i].next)
 46         {
 47             int y=t[i].y;
 48             if(dis[y]>dis[x]+t[i].c)
 49             {
 50                 dis[y]=dis[x]+t[i].c;
 51                 if(!inq[y])
 52                 {
 53                     inq[y]=1;
 54                     q.push(y);
 55                     if(++cnt[y]>n) return 0;
 56                 }
 57             }
 58         }
 59     }
 60     return 1;
 61 }
 62 
 63 int main()
 64 {
 65     int T,kase=0;
 66     scanf("%d",&T);
 67     while(T--)
 68     {
 69         int d;
 70         scanf("%d%d",&n,&d);
 71         for(int i=1;i<=n;i++)
 72         {
 73             scanf("%d",&a[i].x);
 74             a[i].id=i;
 75         }
 76         printf("Case %d: ",++kase);
 77         if(n==1) {printf("0
");continue;}
 78         sort(a+1,a+1+n,cmp);
 79         len=0;
 80         memset(first,0,sizeof(first));
 81         for(int i=1;i<n;i++)
 82         {
 83             ins(a[i].id,a[i+1].id,d);
 84             ins(a[i+1].id,a[i].id,d);
 85         }
 86         for(int i=2;i<=n;i++) ins(i,i-1,-1);
 87         
 88         
 89         if(a[1].id<a[n].id)
 90         {
 91             if(spfa(a[1].id)) printf("%d
",dis[a[n].id]);
 92             else printf("-1
");
 93         }
 94         else
 95         {
 96             if(spfa(a[n].id)) printf("%d
",dis[a[1].id]);
 97             else printf("-1
");
 98         }
 99     }
100     return 0;
101 }
[HDU3440]

2016-04-14 13:50:22

原文地址:https://www.cnblogs.com/Konjakmoyu/p/5390806.html