HDU3440 House Man 【差分約束系統】

來源:互聯網
上載者:User

標籤:hdu3440

House ManTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2056    Accepted Submission(s): 811

Problem DescriptionIn 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.
 
InputIn 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.
 
OutputFor 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
34 4 20 30 10 40 5 6 20 34 54 10 15 4 2 10 20 16 13 
 
Sample Output
Case 1: 3Case 2: 3Case 3: -1
 題意:有N個在一條直線上的房子, 每個房子有著不同的高度, 一個超人可以將這些房子左右移動但不能改變房子之間的相對位置.現在超人要從最矮的房子跳到剛好比他高的房子上面, 且每次跳的房子都要比當前房子要高.那麼最後超人肯定會跳到最高的房子上面, 現在給出超人能夠跳的最遠距離, 問: 如何擺放這些房子, 使得超人能夠經過所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子之間的距離最遠??(摘自討論區)題解:這題關鍵在建圖,建圖時讓橫座標大的點指向橫座標小的點。

隊列:546ms

#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#define inf 0x7fffffff#define maxn 1002using namespace std;int head[maxn], dist[maxn], id, out[maxn];struct Node2{    int v, num;} V[maxn];struct Node{    int to, d, next;} E[maxn * maxn];bool vis[maxn];bool cmp(Node2 a, Node2 b){    return a.v < b.v;}void addEdge(int u, int v, int d){    E[id].to = v; E[id].d = d;    E[id].next = head[u]; head[u] = id++;}int SPFA(int s, int t, int n){    int i, u, v, tmp;    for(i = 0; i <= n; ++i){        dist[i] = inf; vis[i] = 0;        out[i] = 0;    }    u = s; dist[u] = 0; vis[u] = 1;    queue<int> Q; Q.push(u);    while(!Q.empty()){        u = Q.front(); Q.pop(); vis[u] = 0;        if(++out[u] > n) return -1;        for(i = head[u]; i != -1; i = E[i].next){            tmp = dist[u] + E[i].d; v = E[i].to;            if(tmp < dist[v]){                dist[v] = tmp;                if(!vis[v]){                    vis[v] = 1; Q.push(v);                }            }        }    }    return dist[t];}int main(){    int t, n, m, i, cas = 1, u, v;    scanf("%d", &t);    while(t--){        scanf("%d%d", &n, &m);        memset(head, -1, sizeof(head));        for(i = 1, id = 0; i <= n; ++i){            scanf("%d", &V[i].v);            V[i].num = i;            if(i != n) addEdge(i + 1, i, -1);        }        sort(V + 1, V + n + 1, cmp);        for(i = 1; i < n; ++i){            u = V[i].num; v = V[i+1].num;            if(u > v) swap(u, v);            addEdge(u, v, m);        }        printf("Case %d: ", cas++);        u = V[1].num; v = V[n].num;        if(u > v) swap(u, v);        printf("%d\n", SPFA(u, v, n));    }    return 0;}


棧:93ms

#include <stdio.h>#include <string.h>#include <algorithm>#define inf 0x7fffffff#define maxn 1002using namespace std;int head[maxn], dist[maxn];int sta[maxn], id, out[maxn];struct Node2{    int v, num;} V[maxn];struct Node{    int to, d, next;} E[maxn * maxn];bool vis[maxn];bool cmp(Node2 a, Node2 b){    return a.v < b.v;}void addEdge(int u, int v, int d){    E[id].to = v; E[id].d = d;    E[id].next = head[u]; head[u] = id++;}int SPFA(int s, int t, int n){    int i, u, v, tmp, id2;    for(i = 0; i <= n; ++i){        dist[i] = inf; vis[i] = 0;        out[i] = 0;    }    u = s; dist[u] = 0; vis[u] = 1;    id2 = 0; sta[id2++] = s;    while(id2){        u = sta[--id2]; vis[u] = 0;        if(++out[u] > n) return -1;        for(i = head[u]; i != -1; i = E[i].next){            tmp = dist[u] + E[i].d; v = E[i].to;            if(tmp < dist[v]){                dist[v] = tmp;                if(!vis[v]){                    vis[v] = 1; sta[id2++] = v;                }            }        }    }    return dist[t];}int main(){    int t, n, m, i, cas = 1, u, v;    scanf("%d", &t);    while(t--){        scanf("%d%d", &n, &m);        memset(head, -1, sizeof(head));        for(i = 1, id = 0; i <= n; ++i){            scanf("%d", &V[i].v);            V[i].num = i;            if(i != n) addEdge(i + 1, i, -1);        }        sort(V + 1, V + n + 1, cmp);        for(i = 1; i < n; ++i){            u = V[i].num; v = V[i+1].num;            if(u > v) swap(u, v);            addEdge(u, v, m);        }        printf("Case %d: ", cas++);        u = V[1].num; v = V[n].num;        if(u > v) swap(u, v);        printf("%d\n", SPFA(u, v, n));    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.