House MansTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2256 Accepted Submission (s): 896
Problem DescriptionIn Fuzhou, there is a crazy super mans. 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'll start at the shortest house and make N-1 jumps, with each of the jump taking him to a taller house than the one he's Jum Ping from. When finished, he'll has 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 mans want to maximize the distance between the positions of the shortest ho Use and the tallest house.
The Crazy Super man has an ability-move houses. So he's going to move the houses subject to the following constraints:
1. All houses is to be moved along a one-dimensional path.
2. Houses must is moved at integer locations along the path, with no, and houses at the same location.
3. Houses must be arranged so their moved ordering from left to right are the same as their ordering in the input. They must not being sorted by height, or reordered on any. They must is kept in their stated order.
4. The super man can only jump so far, so every house must is moved close enough to the next taller house. Specifically, they must is no further than D apart on the ground (the difference in their heights doesn ' t matter).
Given N houses, in a specified order, with a distinct an integer height, help the Super man figure out the maximum Possi ble distance They can put between the shortest house and the tallest house, and is able to use the houses for training.
Inputin The first line there was an integer T, indicates the number of test cases. (t<=500)
Each test case is begins with a line containing, integers N (1≤n≤1000) and D (1≤d≤1000000). The next line contains n integer, giving the heights of the n houses, and the order that they should is moved. Within a test case, all heights would be unique.
Outputfor each test case, output ' case%d: ' First where D is the ' case ' number counted from one and then output a single Integ Er 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 the houses. Do not print any blank lines between answers.
Sample Input
Sample Output
Case 1:3case 2:3case 3:-1
Authorjyd
Source2010 acm-icpc multi-university Training Contest (1)--host by Fzu
Recommendwe has carefully selected several similar problems for you:3439 3433 3442 3438 3437 topic: HTTP://WWW.CNBLOGS.C Om/scau20110726/archive/2013/05/04/3059625.htmlac Code
#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]);} elseprintf (" -1\n");}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj Topic 3440 House man (differential constraint)