POJ 2904

The Mailboxes Manufacturers Problem

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1135   Accepted: 794

Description

In the good old days when Swedish children were still allowed to blowup their fingers with fire-crackers, gangs of excited kids would plague certain smaller cities during Easter time, with only one thing in mind: To blow things up. Small boxes were easy to blow up, and thus mailboxes became a popular target. Now, a small mailbox manufacturer is interested in how many fire-crackers his new mailbox prototype can withstand without exploding and has hired you to help him. He will provide you with k (1 ≤ k ≤ 10) identical mailbox prototypes each fitting up to m (1 ≤ m≤ 100) crackers. However, he is not sure of how many firecrackers he needs to provide you with in order for you to be able to solve his problem, so he asks you. You think for a while and then say, “Well,if I blow up a mailbox I can’t use it again, so if you would provide me with only k = 1 mailboxes, I would have to start testing with 1 cracker, then 2 crackers, and so on until it finally exploded. In the worst case, that is if it does not blow up even when filled with m crackers, I would need 1 + 2 + 3 + … + m = m × (m + 1) ⁄ 2 crackers. If m = 100 that would mean more than 5000 fire-crackers!” “That’s too many,” he replies. “What if I give you more than k = 1 mailboxes? Can you find a strategy that requires less crackers?”

Can you? And what is the minimum number of crackers that you should ask him to provide you with?

You may assume the following:

  1. If a mailbox can withstand x fire-crackers, it can also withstand x − 1 fire-crackers.
  2. Upon an explosion, a mailbox is either totally destroyed (blown up) or unharmed, which means that it can be reused in another test explosion.

Note: If the mailbox can withstand a full load of m fire-crackers, then the manufacturer will of course be satisfied with that answer. But otherwise he is looking for the maximum number of crackers that his mailboxes can withstand.

Input

The input starts with a single integer N (1 ≤ N ≤ 10) indicating the number of test cases to follow. Each test case is described by a line containing two integers: k and m, separated by a single space.

Output

For each test case print one line with a single integer indicating the minimum number of fire-crackers that is needed, in the worst case, in order to figure out how many crackers the mailbox prototype can withstand.

Sample Input

4
1 10
1 100
3 73
5 100

Sample Output

扫描二维码关注公众号,回复: 10134649 查看本文章
55
5050
382
495

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2002

大致题意:

给出k个油桶,给出油桶被炸掉的最小限度的上限m,问在最坏情况下需要炸药最少的决策条件下需要的炸药。

其中:

1,油桶炸掉之后就损毁了,不能再使用

2,若没炸掉,这下次测试仍可以使用

如果只有一个油桶,那么,只能1个炸药、2个、3个……m个这样测试,所需m*(m+1)/2,m最大为100

解题思路:

首先定义状态dp[i][j][k],

意思为是还剩i个油桶且当前需要进行判断的炸药数量区间是j-k的时候的所使用的炸药总数最优值,

这样的话dp[i][j][k]的情况一定是最坏的我们不能决定,但我们能够进行决策,也就是我们能够确定在当前状态下选择检验的量,对于每个选择的检验量,会有两种情况,

第一种情况是油桶的最小承受限度小于检验量,那么这个油桶就炸掉了,那么就是转移到dp[i-1][j][t-1]+t,(t是本次使用炸药量)

另一种情况最小承受限度大于检验量,那么这个油桶还在,同样我们的检测区间也变小了,也就是转移到dp[i][t+1][k],

因为是在最坏情况下,所以要在两种情况中取较大的,然后因为要通过决策得到最优解,也就是最小值,所以枚举t求解最小值

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 107
#define INF 1<<29
 
using namespace std;
 
int n,k,m;
 
int dp[20][MAX][MAX];
 
void init ( )
{
    memset ( dp , 0 , sizeof (dp));
    for ( int i = 1 ; i < MAX ; i++ )
        for ( int j = i ; j < MAX ; j++ )
            dp[1][i][j] = (j-i+1)*(i+j)/2;
 
    for ( int i = 2 ; i <= 10 ; i++ )
        for ( int j = 1 ; j <= 100 ; j++ )
            for (int k = j; k > 0 ; k-- )
            {
                dp[i][k][j] = INF;
                for ( int t = k ; t <= j ; t++ )
                    dp[i][k][j] = min ( dp[i][k][j] ,
                                       max ( dp[i-1][k][t-1]+t , dp[i][t+1][j]+t ));
            }
}
 
int main ()
{
    init();
    scanf ( "%d" , &n );
    while ( n-- )
    {
        scanf ( "%d%d" , &k , &m );
        printf ( "%d\n" , dp[k][1][m] );
    }
}
发布了158 篇原创文章 · 获赞 34 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_40421671/article/details/95964804
POJ