The Fellowship of the ring
Frodo formed the fellowship of the Ring and is taking the one ring to Mordor.
On the way to Mordor, he encounters N groups of orcs, in the order of 1 to N.
When Frodo bumps into an orc group, he can choose one of the three following options:
Pay the Toll
He can pay a regular toll and pass by them safely.
Recruit a mercenary
If he pays the double of the toll, the corresponding orc group gets hired as mercenaries of the Fellowship of the Ring.
If the number of the Fellowship of the Ring members is greater than or equal to the number of orcs, a battle is possible.
However,
- Frodo does not count as one of the members of the fellowship of the Ring.
- A hired mercenary can participate at maximum 3 battles. Even if the orcs are alive after 3 battles, the group gets dispersed.
- In a battle, independent of the number of orcs, all hired mercenaries participate in the battle.
- The battle goes on until the orc group gets exterminated. The Fellowship of the Ring, will, as well, loose the same number of soldiers.
- In the Fellowship of the Ring, soldiers get killed in the order of who got hired first.
#Given N, the number of orc groups and the toll, find the minimum cost to get to Mordor.
For instance, suppose there are 7 (N=7) orc groups.
(Yellow stands for the number of orcs in the group and white stands for the toll).
1 | 2 | 3 | 4 | 5 | 6 | 7 | ||||||
10 | 70 | 80 | 20 | 50 | 30 | 10 | ||||||
$100 | $5 | $15 | $60 | $90 | $80 | $10 |
The minimum cost to get to Mordor is 15$
1 | 2 | 3 | 4 | 5 | 6 | 7 | ||||||
10 | 70 | 80 | 20 | 50 | 30 | 10 | ||||||
$100 | $5 | $15 | $60 | $90 | $80 | $10 | ||||||
pass | hire | hire | battle | battle | battle | pass | ||||||
100$ | 110$ | 140$ | 150$ |
If there are 11(N=11) orc groups, the minimum cost becomes$2370.
Orc group | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | |
The number | 1000 | 700 | 400 | 300 | 900 | 300 | 50 | 50 | 700 | 500 | 50 | |
Toll | 10 | 900 | 500 | 10 | 900 | 10 | 900 | 900 | 900 | 900 | 10 | |
Choose | Hire | Battle | Pass | Hire | Pass | Hire | Battle | battle | Pass | Battle | Pass | |
Cost($) | 20 | 20 | 520 | 540 | 1440 | 1460 | 1460 | 1460 | 2360 | 2360 | 2370 | |
Current Orc | 1000 | 300 | 300 | 600 | 600 | 900 | 850 | 800 | 600 | 100 | 0 | |
Group 1 | 1000 | 300 | 300 | 300 | 300 | 300 | 250 | 200 |
|
|
| |
Group 4 |
|
|
| 300 | 300 | 300 | 300 | 300 | 300 | 0 |
| |
Group 6 |
|
|
|
|
| 300 | 300 | 300 | 300 | 100 |
|
[Constraints]
- 5<=N<=20
- The number of orcs in each group is greater than or equal to 1 and less than or equal to 1000.
- The toll of each orc group is each group is greater than or equal to 1 and less than or equal to 1000.
[Input]
The first line contains a single integer T-number of total test cases.
Below, each test case is given.
The first line of each test case contains N- the number of orc groups.
The next N lines contain the number of orcs and the toll, respectively.
[Output]
Print the respective answers for T test case in total for T line.
Each line starts with “#x”, gives one space, and then output the answer. (X is the test case number starting form 1)
input | Output |
5 7 //Test Case 1, N =7 10 100 70 5 80 15 20 60 50 90 30 80 10 10 9 //Test Case 2, N =9 600 800 300 400 300 400 1000 400 300 600 100 300 600 300 600 500 1000 300 11 //Test Case 2, N =11 1000 10 700 900 400 500 300 10 900 900 300 10 50 900 50 900 700 900 600 900 50 10 20 //Test Case 4, N =20 543 216 454 310 408 367 40 602 252 582 954 627 850 234 763 479 232 278 301 538 528 508 936 154 629 443 758 336 432 700 882 256 278 738 517 882 317 136 20 //Test Case 5, N =20 410 610 831 909 675 629 421 774 386 869 544 219 492 414 996 557 499 482 231 785 804 978 304 881 489 911 75 315 927 648 252 914 300 396 937 133 495 882 813 717 | #1 150 #2 3000 #3 2370 #4 4721 #5 8231 |
#include <stdio.h>
#define N 20
struct Orc {
int a[3];
int sum;
};
int n, g;
int cost[N], quan[N];
int min(int a, int b) { return a < b ? a : b; }
void dfs(int k, int s, Orc orc) {
if (s >= g) return;
if (k == n) {
g = s;
return;
}
if (orc.sum >= quan[k]) {
Orc t = orc;
int x = quan[k];
int i = 2;
while (x) {
int y = min(t.a[i], x);
t.a[i] -= y;
x -= y;
i--;
}
t.sum -= quan[k];
t.sum -= t.a[2];
t.a[2] = t.a[1];
t.a[1] = t.a[0];
t.a[0] = 0;
dfs(k + 1, s, t);
}
s += cost[k];
dfs(k + 1, s, orc);
s += cost[k];
orc.a[0] += quan[k];
orc.sum += quan[k];
dfs(k + 1, s, orc);
}
int main() {
int t, T;
t = 0;
scanf("%d", &T);
while (t++ < T) {
scanf("%d", &n);
g = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d", &quan[i], &cost[i]);
g += cost[i];
}
Orc orc;
orc.sum = 0;
for (int i = 0; i < 3; i++) orc.a[i] = 0;
dfs(0, 0, orc);
printf("#%d %d\n", t, g);
}
return 0;
}