首爾性愛美術館18禁的藝術三星平板★瘋殺5290起攝影界美人正妹指定相機款睽違25天終開會 立院...
2013-06-29 21:46:08 人氣(130) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][dp] 11157 - Dynamic Frog

0
收藏
0
推薦

D

Next Generation Contest 3

Time Limit: 2 seconds

Dynamic Frog

 

With the increased use of pesticides, the local streams and rivers have become so contaminated that it has become almost impossible for the aquatic animals to survive.

Frog Fred is on the left bank of such a river. N rocks are arranged in a straight line from the left bank to the right bank. The distance between the left and the right bank is D meters. There are rocks of two sizes. The bigger ones can withstand any weight but the smaller ones start to drown as soon as any mass is placed on it. Fred has to go to the right bank where he has to collect a gift and return to the left bank where his home is situated.

He can land on every small rock at most one time, but can use the bigger ones as many times as he likes. He can never touch the polluted water as it is extremely contaminated.

Can you plan the itinerary so that the maximum distance of a single leap is minimized?

 

Input

The first line of input is an integer T(T<100) that indicates the number of test cases. Each case starts with a line containing two integers N(0≤N≤100)and D(1≤D≤1000000000). The next line gives the description of the N stones. Each stone is defined by S-M. S indicates the type Big(B) or Small(S) and M(0<M<D) determines the distance of that stone from the left bank. The stones will be given in increasing order of M.

Output

For every case, output the case number followed by the minimized maximum leap.

Sample Input

Output for Sample Input

3
1 10
B-5
1 10
S-5
2 10
B-3 S-6
Case 1: 5
Case 2: 10
Case 3: 7

 

Problem Setter: Sohel Hafiz
Special Thanks: Jane Alam Jan


題目描述:

大石頭可以無限踩, 小石頭只能採一次, 青蛙從左岸跳到右岸, 再跳回左岸,
問其中最大的步伐的最小值為何?

題目解法:

先說明一下, Greedy 也是行得通的, 但這裡我使用 dp 去解決,
小石頭當作一個點, 大石頭當作兩個點, 排序過後,
相當於一次從左岸走兩條路徑到達右岸,
dp[i][j] 表示第一條路徑到達 i 且 第二條路徑到達 j, 轉換方程如下表示。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main() {
    int testcase, n, D, cases = 0;
    char s[50];
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &n, &D);
        int A[305], m = 0, x;
        for(i = 0; i < n; i++) {
            scanf("%s", s);
            sscanf(s+2, "%d", &x);
            A[m++] = x;
            if(s[0] == 'B')
                A[m++] = x;
        }
        A[m++] = 0;
        A[m++] = D, A[m++] = D;
        sort(A, A+m);
        int dp[305][305];
#define oo 2147483647
        for(i = 0; i < m; i++)
            for(j = 0; j < m; j++)
                dp[i][j] = oo;
        dp[0][0] = 0;
        for(i = 0; i < m; i++) {
            for(j = 0; j < m; j++) {
                k = max(i, j)+1;
                dp[i][k] = min(dp[i][k], max(dp[i][j], A[k]-A[j]));
                dp[k][j] = min(dp[k][j], max(dp[i][j], A[k]-A[i]));
            }
        }
        printf("Case %d: %d\n", ++cases, dp[m-1][m-2]);
    }
    return 0;
}

11157Dynamic Frogdp
台長:Morris
人氣(130) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11308 - Bankrupt Baker
此分類上一篇:[UVA][遞迴] 11147 - KuPellaKeS BST

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文