Problem Statement
Each time we choose a component instance, we first determine if it can be installed. Some components may require one or more other types of components to already be installed. If all the required components are not currently installed, we cannot install the chosen component.
If the chosen component can be installed, we then decide whether or not to actually install it. Each component has an associated income and expense. The expense is the amount of money we must spend to install the component, and the income is the amount of money the installed component is expected to give us. Our goal is to maximize the expected total profit (all our income minus all our expenses). If we decide to install a chosen component, we must install it immediately (i.e., we can't put it aside and use it later).
Multiple instances of the same component type can be installed in the application. Each instance costs the same amount of money and has the same expected income. Note that once all the requirements for a component have been met, we may install multiple instances of that component type. For example, consider the case where component type 5 requires component type 3, and a single instance of component type 3 is currently installed. Every time we choose component type 5 from now on, we are allowed to install an instance of it. We do not require an instance of component type 3 for each instance of component type 5.
You are given an int k, the number of times we randomly choose a new component. You are also given a
Definition
- Class:
- CrazyComponents
- Method:
- expectedProfit
- Parameters:
- int, String[], int[], int[]
- Returns:
- double
- Method signature:
- double expectedProfit(int k, String[] components, int[] income, int[] expense)
- (be sure your method is public)
Notes
- A return value with either an absolute or relative error of less than 1.0E-9 is considered correct.
Constraints
- k will be between 1 and 50, inclusive.
- components will contain between 1 and 15 elements, inclusive.
- components, income and expense will each contain the same number of elements.
- Each element of components will be a single space separated list of integers, without leading or trailing spaces.
- Each integer in each element of components will be between 0 and n-1, inclusive, with no extra leading zeroes, where n is the number of elements in components.
- The list described by element i of components will not contain i.
- In each element of components, integers will be distinct.
- Each element of income will be between 0 and 1000000, inclusive.
- Each element of expense will be between 0 and 1000000, inclusive.
Examples
1
{ "", "" }
{ 1, 2 }
{ 0, 0 }
Returns: 1.5
Here, we choose a component only once. If we choose component type 0, our profit is 1 - 0 = 1. If we choose component type 1, our profit is 2 - 0 = 2. Since each type is equally likely to be chosen, the expected profit is 0.5 * 1 + 0.5 * 2.
50
{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }
{ 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 }
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 50000.0
17
{ "1 2 3 4 5", "", "", "", "", "" }
{ 1000, 0, 0, 0, 0, 0 }
{ 0, 100, 100, 100, 100, 100 }
Returns: 301.23489487382017
23
{ "1 2 3", "2 3", "3", "" }
{ 987654, 12345, 12345, 12345 }
{ 123456, 98765, 98765, 98765 }
Returns: 2178721.482113228
50
{ "1 2 3", "", "" , "" }
{ 1000, 0, 0, 0 }
{ 0, 333, 333, 333 }
Returns: 9667.669182645035
50
{ "3 5 8 1", "3 5 8", "9 6 3", "1 2", "", "9 8", "", "", "", "", "", "", "", "", "" }
{ 1423, 23423, 234523, 234234, 64575, 65578, 756865, 756856, 568679, 346457, 4564, 97895, 867864, 4786, 457586 }
{ 83, 635745, 3454, 346435, 345645, 3468, 35765, 34647, 64589, 7536 ,8563, 546 ,74645, 87635, 3657 }
Returns: 1.2220512264046831E7
12
{ "1", "0" }
{ 345774, 965234 }
{ 123, 654 }
Returns: 0.0
50
{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "0" }
{ 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999, 999999 }
{ 123, 23423, 3453, 234534, 234534, 34534, 234534, 234534, 345, 74567, 575, 3457, 4754, 7543, 8564 }
Returns: 0.0
42
{ "10", "2 14 12", "", "1 10 7 4 2", "13", "4", "1 12", "9 10 11 12 8", "9", "", "11 13", "7 1 12 2", "0 14", "0 14 7", "2 11" }
{ 229256, 395321, 430193, 896648, 943156, 748288, 376108, 286526, 619176, 292469, 801614, 372602, 612005, 464916, 114101 }
{ 184265, 514178, 761731, 254337, 840235, 446818, 418671, 946273, 305527, 744361, 875794, 412824, 275437, 975798, 357870 }
Returns: 189550.05923618007
43
{ "12", "2", "7 0 14 13", "", "14 0 9 5 13", "13 6", "10 12 3", "3", "", "4", "14", "4 3 2 0", "9", "3 5 4 2", "4 5 6" }
{ 448113, 547049, 662039, 77755, 381337, 391766, 72517, 473840, 255847, 328965, 44416, 410482, 172106, 421985, 197300 }
{ 671539, 806844, 396175, 718543, 992876, 801451, 252173, 696007, 975537, 443593, 833995, 53418, 390350, 719127, 148916 }
Returns: 0.0
44
{ "3", "8 6", "0", "7 1 5 12 10", "5", "2 6 14", "", "", "14 7 13 9", "0 4 7 10 5", "", "10 8 0", "13 1 3", "3 8 4 7 5", "6 11 2 10 8" }
{ 974739, 797734, 963559, 674092, 87484, 220574, 277282, 232154, 863107, 790110, 376332, 495049, 476990, 790863, 160069 }
{ 189007, 515008, 999909, 192599, 668654, 153381, 521205, 425561, 649337, 785547, 697987, 156402, 623509, 835575, 633660 }
Returns: 0.0
42
{ "", "3 14 4 6 5", "8 9 10 4", "", "", "", "9 11", "", "4 9 1 10 13", "10 11", "5", "9 5 10", "6 11", "3 11 4", "8" }
{ 15858, 47122, 825040, 820399, 342310, 398546, 672039, 152315, 843510, 64092, 443629, 344869, 93030, 11182, 481422 }
{ 934753, 459759, 324240, 384639, 140971, 46286, 293139, 679853, 363758, 53438, 239673, 515841, 60976, 695786, 976655 }
Returns: 3148574.1501256498
45
{ "11", "9 5 2 11", "8 4 9 6", "0 11 1 14 7", "", "3 8 9 14 1", "10 7 9", "", "10 5", "0 7 2 1", "", "13 8 7", "3", "3 1", "4 9 0" }
{ 978629, 468727, 983860, 882188, 427243, 550963, 497412, 973377, 837744, 184125, 143647, 129151, 793970, 474056, 566361 }
{ 196384, 879062, 258861, 160049, 585377, 884323, 202211, 264308, 222208, 129988, 28369, 432749, 64353, 565239, 424228 }
Returns: 2473041.000000001
44
{ "11 8", "2 8", "3 7 11 9 4", "8", "5 11", "11", "12 5 9", "10 4 9 8", "5 9", "4 7 1 14 2", "3", "", "", "4 10 6", "3" }
{ 457806, 643689, 230778, 649545, 832009, 145582, 356133, 743012, 324548, 247901, 395226, 21056, 693215, 919796, 642196 }
{ 679770, 85591, 996412, 762579, 120881, 755966, 637006, 37121, 211895, 883047, 480282, 217935, 587615, 151504, 970783 }
Returns: 560969.7690987464
40
{ "5", "0 5", "", "0 11", "13 11", "14 7 9 10 13", "3 7 2 8", "14", "11 0 12 4 10", "3 2", "8 3", "", "9 1 8 5", "12 2 4 5 14", "1 4 8" }
{ 333974, 792520, 633052, 655732, 93238, 950068, 906626, 605091, 750577, 906671, 777716, 904297, 497551, 307029, 753767 }
{ 314365, 324719, 362602, 956819, 701733, 680363, 105543, 390901, 119974, 721763, 762997, 852333, 181805, 667116, 860119 }
Returns: 859770.6666666653
41
{ "3 14", "5", "", "", "3", "1 6", "", "10 1 4 14", "9", "11", "11 1", "3 12 13", "", "", "5 1 0 13 3" }
{ 784459, 647755, 142184, 512514, 215087, 685416, 171277, 136220, 484289, 452533, 125968, 256495, 350133, 667271, 658304 }
{ 522567, 459520, 34887, 933290, 516830, 437602, 207983, 553095, 579234, 851522, 945435, 607842, 12715, 58371, 604170 }
Returns: 2879881.0
48
{ "6 8 3 9 4", "3 6 10", "", "13", "11 0", "9 10", "7", "5 1 4", "10 11", "4", "7 4 3 12", "", "13", "7", "" }
{ 685103, 112714, 401575, 328364, 316344, 392949, 335869, 71572, 293075, 959491, 855489, 367342, 963132, 454769, 897440 }
{ 408595, 636222, 3893, 904271, 691761, 686725, 183632, 600716, 101905, 971909, 384766, 749855, 981846, 507652, 493100 }
Returns: 2566470.3999999906
45
{ "11", "2 6", "1 3 4 5 13", "", "", "11 10 6 1", "", "8 9", "10 2", "8 2", "5", "13 6 14 5 0", "9 1", "9 1 3 10 2", "12 9 1 4 6" }
{ 768391, 744893, 917614, 267958, 162632, 632549, 629706, 829291, 178119, 67739, 710900, 482784, 7813, 597084, 790990 }
{ 55138, 244264, 459419, 72460, 553365, 942461, 177804, 164954, 214499, 720479, 382078, 948966, 818032, 345270, 493519 }
Returns: 1942200.0
46
{ "7", "3 8", "4", "6 4 14 5", "12 9 7 3 14", "10", "", "4 0 12 5", "", "13 12 2 0", "12 2", "", "4 5 6 1", "", "3 4 5 6 7" }
{ 391551, 899880, 509987, 586564, 804714, 732105, 465647, 344003, 904993, 430473, 765989, 51655, 261052, 627489, 469915 }
{ 534219, 843149, 275183, 641196, 74280, 141023, 983507, 575980, 459798, 907674, 742294, 521973, 410499, 576481, 189118 }
Returns: 1521689.1999999997
43
{ "9 10 11 12 6", "", "1", "1 0", "13 14 1", "1 2 8", "", "10 3 11 1 2", "9 3", "12 7 4 0 3", "9 12 13 14 0", "12 1 13", "0", "10 4", "12" }
{ 367716, 214576, 644708, 494246, 533212, 713121, 389058, 645093, 262305, 355812, 461853, 92651, 137505, 577176, 63023 }
{ 249554, 418314, 353333, 98339, 282238, 22880, 447448, 432137, 423600, 704676, 102782, 799059, 621464, 361022, 779526 }
Returns: 371104.79839974205
48
{ "7 1 13 2", "4 6 2", "0 12 8 14 11", "11", "", "", "14 5 4 12", "6 8 10", "", "2", "12 11 5 8 7", "10 5", "0 1", "2 14 0 5 7", "3 4 2 5" }
{ 576119, 289454, 121592, 790183, 403584, 144293, 158792, 549603, 432905, 600, 652274, 918172, 889173, 272519, 241213 }
{ 191379, 740866, 300069, 28947, 464557, 879732, 677402, 765927, 721992, 306535, 436028, 7291, 953405, 602682, 288650 }
Returns: 0.0
47
{ "5 6 12 9 1", "", "12 13 11 3", "5 6", "1", "", "5", "13", "9 2 7", "1 0 4 2", "3 9 7 8", "2 3 12", "14", "1", "13 0 9" }
{ 140555, 738064, 611419, 379003, 572763, 556377, 837971, 705199, 42892, 757185, 356088, 150012, 812396, 440048, 859029 }
{ 715460, 350029, 62008, 182808, 163652, 760220, 290486, 875023, 83049, 268110, 201113, 165668, 55811, 162737, 168705 }
Returns: 3965947.200349608
42
{ "", "4 14 3 2", "10 1 3 7 4", "", "12 3", "4 9 8 10 6", "3", "2 8 3", "14 2 7 10", "6 7 4 11 3", "2", "7 3 13 0 5", "14 3 2", "3 7 0 8", "1 10 8 2" }
{ 33753, 652505, 662782, 517530, 959609, 903624, 112150, 244441, 536895, 850111, 382780, 889248, 855875, 498595, 936192 }
{ 496809, 757028, 148328, 984774, 807823, 356909, 169905, 388632, 559436, 694171, 220856, 568537, 528221, 217483, 960127 }
Returns: 0.0
40
{ "1", "3", "5", "4 5", "10 3", "6 4 10", "", "6 8 2", "5 4 11 13 14", "0 3 10 11", "8", "9 7 4", "11 1 13 0 3", "6 8 7 0 10", "0 13 10 12 1" }
{ 239361, 701576, 986280, 78932, 931163, 265235, 991764, 263475, 429419, 109471, 554583, 802197, 671180, 266089, 513922 }
{ 302321, 376874, 339214, 633537, 465991, 831086, 320310, 411689, 938982, 169210, 367286, 790637, 845023, 25609, 851254 }
Returns: 1790543.9999999974
47
{ "5 2 3 13 1", "10", "3 4 6", "6 8", "0 12 1 2", "8 10 7 6", "5 3", "8", "11 7 12", "0", "11", "7 8 1 4", "", "12 8 3 7 14", "0 2 10" }
{ 272198, 173990, 291227, 4575, 149839, 332847, 121923, 5234, 285582, 350797, 840164, 200765, 849714, 329891, 436943 }
{ 19157, 635424, 985709, 828218, 866418, 362129, 714470, 642584, 804240, 349810, 932663, 224305, 439658, 620847, 863281 }
Returns: 1284842.133333329
41
{ "13 2 7 8", "0 2", "0", "", "8 0 3 9", "3", "13 9 4 14 12", "", "1 14 2", "", "5 3 7 8 9", "", "", "", "" }
{ 692413, 385179, 918448, 362237, 442176, 438196, 595499, 25597, 509248, 231245, 438059, 847772, 176721, 499619, 987003 }
{ 653684, 151884, 638419, 923752, 909768, 273694, 362293, 553329, 904942, 257372, 684118, 217697, 712148, 893489, 732730 }
Returns: 2417217.866666665
49
{ "9 10", "6 7", "13", "9 12", "3 11 13 12", "9 10 11", "13 8 1", "", "1 14 11", "0 1", "11 3 0", "5 7 12", "1 5", "1 14 6 12", "1 3 0 9 10" }
{ 474093, 52963, 35289, 427393, 755486, 202916, 747816, 638405, 708590, 664629, 901437, 773701, 987500, 792059, 967961 }
{ 709156, 406977, 768848, 267832, 627805, 554419, 256187, 281214, 383572, 337250, 971759, 967705, 712088, 816625, 216735 }
Returns: 1166823.9333333345
50
{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "" }
{ 2000, 1900, 1800, 1700, 1600, 1500, 1400, 1300, 1200, 1100, 1000, 900, 800, 700, 600 }
{ 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123 }
Returns: 5312.184960911477
50
{ "1 2 3 4 5 6 7 8 9 10 11 12 13 14", "2 3 4 5 6 7 8 9 10 11 12 13 14", "3 4 5 6 7 8 9 10 11 12 13 14", "4 5 6 7 8 9 10 11 12 13 14", "5 6 7 8 9 10 11 12 13 14", "6 7 8 9 10 11 12 13 14", "7 8 9 10 11 12 13 14", "8 9 10 11 12 13 14", "9 10 11 12 13 14", "10 11 12 13 14", "11 12 13 14", "12 13 14", "13 14", "14", "" }
{ 2000, 1900, 1800, 1700, 1600, 1500, 1400, 1300, 1200, 1100, 1000, 900, 800, 700, 600 }
{ 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123 }
Returns: 5312.184960911477
50
{ "1 2 5 13 14", "2 3 4 8 12 13 14", "3 4 5 6 7 8 13 14", "4 5 6 7 8 9 10 14", "5 6 9 10 11 12 13 14", "6 7 8 12 13 14", "7 8 9 10 13 14", "", "9 10 11 12 13 14", "10 11 12 13 14", "11 14", "12 13", "", "14", "" }
{ 576119, 289454, 121592, 790183, 403584, 144293, 158792, 549603, 432905, 600, 652274, 918172, 889173, 272519, 241213 }
{ 55138, 244264, 459419, 72460, 553365, 942461, 177804, 164954, 214499, 720479, 382078, 948966, 818032, 345270, 493519 }
Returns: 1519653.651185118
2
{ "1", "" }
{ 10, 0 }
{ 0, 2 }
Returns: 1.5
If we pick component 0 in the first turn (1/2 probability), we won't be able to install it. Whichever component we pick after that, it's better not to install it and settle for 0 profit. On the other hand, if we pick component 1 in the first turn (1/2 probability), we should install it even though the net is -2. If on the second try we pick component 1 again (1/4 total probability), we don't install it and end up losing the 2 we already spent. However, if we pick component 0 on the second try (1/4 total probability), we can install it and earn 10 for a total profit of 8. The expected profit is 1/4 * (-2) + 1/4 * 8 = 1.5.
3
{ "1 2", "", "" }
{ 100, 0, 0 }
{ 0, 0, 0 }
Returns: 7.407407407407408
Component 0 (the only one that makes a profit) requires components 1 and 2 to be already installed, so if we draw it in the first or in the second turn, we won't be able to install it. It is only possible to install it in the 3rd turn, supposing that in the 1st and 2nd turns we drew components 1 and 2. The probability that we will have components 1 and 2 installed after the 2nd turn is 2/3 * 1/3 = 2/9. The probability of then picking component 0 is 1/3, so our expected profit equals 2/9 * 1/3 * 100 = 7.407... .
5
{ "1", "2", "0", "" }
{ 4, 5, 6, 7 }
{ 0, 0, 0, 8 }
Returns: 0.0
10
{ "", "", "", "", "", "", "", "", "", "" }
{ 142352, 2342, 34534, 2344, 12346, 7589, 79872, 973453, 96233, 34567 }
{ 12432, 2345, 3678, 456, 345, 457, 56758, 4564, 774, 34567 }
Returns: 1269258.9999999998
1
{"", "" }
{1, 2 }
{0, 0 }
Returns: 1.5
10
{"", "", "", "", "", "", "", "", "", "" }
{142352, 2342, 34534, 2344, 12346, 7589, 79872, 973453, 96233, 34567 }
{12432, 2345, 3678, 456, 345, 457, 56758, 4564, 774, 34567 }
Returns: 1269258.9999999998
49
{"1 2 5 9", "2 3 4", "", "0", "", "", "", "", "", "8" }
{8, 10, 1, 2, 3, 12, 23, 21, 4, 0 }
{0, 2, 0, 9, 2, 3, 9, 213, 3, 2 }
Returns: 127.40000000000039
5
{"3", "", "", "", "1" }
{4, 0, 3, 2, 7 }
{9, 123, 0, 5, 2 }
Returns: 2.999999999999999
3
{"1 2", "", "" }
{100, 0, 0 }
{0, 0, 0 }
Returns: 7.407407407407408
6
{"1 2 3 4", "4", "1 3 4", "1 4", "", "1 3 4" }
{20, 25, 12, 7, 10, 11 }
{8, 10, 14, 18, 5, 10 }
Returns: 10.023469650205762
10
{"", "", "1", "2", "8", "7", "1", "9", "3", "" }
{0, 0, 0, 9, 1, 1, 1, 1, 2, 3 }
{3, 2, 1, 1, 1, 1, 1, 1, 1, 1 }
Returns: 2.0000000000000004