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.
题意:有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动但不能改变房子之间的相对位置.现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高.那么最后超人肯定会跳到最高的房子上面, 现在给出超人能够跳的最远距离, 问: 如何摆放这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子之间的距离最远??(摘自讨论区)
题解:关键在于建图,不能乱建图,坐标小的才可以到达坐标大的,这样建图才可以。
然后就是线性差分约束了。
1 #include<cstring> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<cstdio> 6 #include<queue> 7 #define INF 2000000007 8 using namespace std; 9 10 int n,m,S,T,Case; 11 int dis[1007],ins[1007],num[1007]; 12 int cnt,head[1007],Next[10007],rea[10007],val[10007]; 13 struct Node 14 { 15 int x,num; 16 }a[1007]; 17 18 bool cmp(Node x,Node y) 19 { 20 return x.x<y.x; 21 } 22 void Spfa() 23 { 24 for (int i=1;i<=n;i++) 25 dis[i]=INF,ins[i]=0; 26 dis[S]=0,ins[S]=1; 27 queue<int>q; 28 q.push(S); 29 while(!q.empty()) 30 { 31 int u=q.front();q.pop(); 32 for (int i=head[u];i!=-1;i=Next[i]) 33 { 34 int v=rea[i],fee=val[i]; 35 if (dis[v]>fee+dis[u]) 36 { 37 dis[v]=fee+dis[u]; 38 if (!ins[v]) 39 { 40 ins[v]=1; 41 q.push(v); 42 num[v]++; 43 if (num[v]>n) 44 { 45 dis[T]=-1; 46 return; 47 } 48 } 49 } 50 } 51 ins[u]=0; 52 } 53 } 54 void add(int u,int v,int fee) 55 { 56 Next[++cnt]=head[u]; 57 head[u]=cnt; 58 rea[cnt]=v; 59 val[cnt]=fee; 60 } 61 int main() 62 { 63 int Cas;scanf("%d",&Cas); 64 while(Cas--) 65 { 66 cnt=0; 67 memset(head,-1,sizeof(head)); 68 memset(num,0,sizeof(num)); 69 scanf("%d%d",&n,&m); 70 for (int i=1;i<=n;i++) 71 { 72 scanf("%d",&a[i].x); 73 a[i].num=i; 74 if (i!=1) add(i,i-1,-1); 75 } 76 sort(a+1,a+n+1,cmp); 77 for (int i=1;i<n;i++) 78 { 79 int x=a[i].num,y=a[i+1].num; 80 if (x>y) swap(x,y); 81 add(x,y,m); 82 } 83 S=a[1].num; 84 T=a[n].num; 85 if (S>T) swap(S,T); 86 Spfa(); 87 printf("Case %d: %d\n",++Case,dis[T]); 88 } 89 }