Problem 1019 - The Mailboxes Manufacturers Problem

/*

Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty:
Total Submit:
33  Accepted: 11  Special Judge: No
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 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
55
5050
382
495

*/

这题大意是有n个完全一样的邮筒,进行爆破测试,最大火药量为m。求测出邮筒实际能承受的火药量所需的最小火药量。这里首先,如果只有一个邮筒,那肯定得从1开始一个一个的试。不然从中间试爆了就完了。。。如果有多个就可以从中间开始试验了。这里用dp。dp[k][i][j]表示有k个邮筒从i到j的火药量测试所需最小的火药数。边界为:

dp[1][i][j]=i+(i+1)+(i+2)+……+j=(j-i+1)*(i+j+/2

有k个邮筒时在i到j之间直接测试t。有两种情况,

第一种。炸了。。所以此时所需火药为t+dp[k-1][i][t-1];

第二种。没事。。此时为t+dp[k][t+1][j];

这题有多组数据,可以先将范围内所有情况都算出来,然后直接显示结果。下面是AC代码。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define INF 1000000003
 5 int dp[12][110][110];
 6 int main()
 7 {
 8     int i,j,k,t,max,N,n,m;
 9     for(i=1;i<=100;i++)
10         for(j=i;j<=100;j++)
11             dp[1][i][j]=(i+j)*(j-i+1)/2;
12     for(k=2;k<=10;k++)
13         for(j=100;j>=1;j--)
14             for(i=j;i>=1;i--)
15             {
16                 dp[k][i][j]=INF;
17                 for(t=i;t<=j;t++)
18                 {
19                     max=(dp[k-1][i][t-1]>dp[k][t+1][j]?dp[k-1][i][t-1]:dp[k][t+1][j]);
20                     dp[k][i][j]=(dp[k][i][j]<(t+max)?dp[k][i][j]:(t+max));
21                 }
22             }
23     scanf("%d",&N);
24     while(N--)
25     {
26         scanf("%d%d",&n,&m);
27         printf("%d\n",dp[n][1][m]);
28     }
29     return 0;
30 }

 

posted @ 2013-08-17 10:56  hjf007  阅读(224)  评论(0编辑  收藏  举报