Skip to content

Commit 2302f38

Browse files
authored
Merge pull request ashutosh97#47 from David-Hinschberger/master
Added Mailbox Manufacturers Problem
2 parents c794063 + 8460142 commit 2302f38

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
The Mailbox Manufacturers Problem
2+
3+
In the good old days when Swedish children were still allowed to blow up 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.
4+
5+
However, he is not sure of how many fire-crackers 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:
6+
7+
“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!”
8+
9+
“That’s too many”, he replies. “What if I give you more than k=1 mailboxes? Can you find a strategy that requires fewer fire crackers?”
10+
11+
Can you? And what is the minimum number of crackers that you should ask him to provide you with?
12+
13+
You may assume the following:
14+
15+
If a mailbox can withstand x fire-crackers, it can also withstand x−1 fire-crackers.
16+
17+
Upon an explosion, a mailbox is either totally destroyed (blown up) or unharmed, which means that it can be reused in another test explosion.
18+
19+
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.
20+
21+
Input
22+
The input starts with a single integer N (1≤N≤100) 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.
23+
24+
Output
25+
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.
26+
27+
Sample Input
28+
4
29+
1 10
30+
1 100
31+
3 73
32+
5 100
33+
34+
Sample Output
35+
55
36+
5050
37+
382
38+
495
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int memo[11][100][100] = {0};
5+
6+
int solve(int k, int start, int end){
7+
if(k == 0){
8+
return INT_MAX;
9+
}
10+
if(k == 1){
11+
return (end * (end + 1) / 2 - (start * (start + 1) / 2));
12+
}
13+
if(start == end){
14+
return 0;
15+
}
16+
17+
if(memo[k][start][end] == 0){
18+
int result = INT_MAX;
19+
for(int i = start + 1; i <= end; i++){
20+
result = min(result, i + max(solve(k-1, start, i-1), solve(k, i, end)));
21+
}
22+
memo[k][start][end] = result;
23+
}
24+
return memo[k][start][end];
25+
}
26+
27+
int main(){
28+
int nCases;
29+
cin >> nCases;
30+
while(nCases--){
31+
int mailboxes, firecrackers;
32+
cin >> mailboxes >> firecrackers;
33+
cout << solve(mailboxes, 0, firecrackers) << endl;
34+
}
35+
return 0;
36+
}

0 commit comments

Comments
 (0)