Problem Statement
A snail was racing along a straight trail for raceTime seconds. The snail maintained the same direction during the entire race. On the other hand, its speed during the race was not necessarily constant.
The race had observerCount observers. Each of them watched the snail during a single contiguous closed interval that measured exactly observationTime seconds.
(No observation started before the start of the race. No observation ended after the end of the race. Some observers could have started and therefore also ended their observation at a non-integer timestamp.)
It is guaranteed that during the entire race the snail was always observed by at least one person. Each observer came to the same conclusion: the distance traveled by the snail during their observation was exactly observationDistance inches.
Return the maximum distance in inches the snail could have traveled during the entire race.
Definition
- Class:
- WatchedSnail
- Method:
- maxDistance
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double maxDistance(int raceTime, int observerCount, int observationTime, int observationDistance)
- (be sure your method is public)
Notes
- Your return value will be accepted if it has an absolute or a relative error at most 10^(-9).
- The snail cannot teleport (move at infinite speed). Its position in time is a continuous non-decreasing function.
- As implied by the "non-decreasing" in the previous sentence, the snail may stand still during some intervals of time.
Constraints
- raceTime will be between 1 and 1000, inclusive.
- observerCount will be between 1 and 1000, inclusive.
- observationTime will be between 1 and raceTime, inclusive.
- observationDistance will be between 1 and 1000, inclusive.
- observerCount * observationTime will be greater than or equal to raceTime.
Examples
100
3
100
47
Returns: 47.0
Each person observed the snail during the entire race and saw the snail traverse 47 inches.
100
10
10
47
Returns: 470.0
Ten people took turns watching the snail. Each person saw the snail traverse 47 inches, so the snail traversed 470 inches total. Boring.
100
8
20
47
Returns: 376.0
This should be obvious, right? Each observation took 20 seconds. The distance traveled during each observation is 47 inches, so for each observation the snail had an average speed of 2.35 inches per second. The whole race was 100 seconds and at that average speed the snail must have travelled 2.35 * 100 = 235 inches. The snail was always watched, so it could not have... wait, what?
100
2
74
47
Returns: 94.0
One observer started at the beginning of the race and one ended at the end of the race. If [0,100] is the time interval of the race, the first observer was active during [0,74] and the second one during [26,100]. It is possible that the snail traversed only 47 inches total, but it is also possible that it traversed more. The largest possible total distance it could have travelled is obviously 94 inches. The answer would clearly remain 94 inches even if we had more than two observers (while keeping the other three parameters the same).
60
47
10
10
Returns: 100.0
60
7
10
10
Returns: 70.0
6
2
4
567
Returns: 1134.0
8
10
6
264
Returns: 528.0
9
7
7
631
Returns: 1262.0
1
7
1
981
Returns: 981.0
2
1
2
966
Returns: 966.0
8
9
1
940
Returns: 8460.0
6
10
4
236
Returns: 472.0
4
9
3
453
Returns: 906.0
6
10
3
572
Returns: 1144.0
10
4
4
808
Returns: 3232.0
1
3
1
101
Returns: 101.0
6
10
4
521
Returns: 1042.0
9
5
2
665
Returns: 3325.0
4
6
2
483
Returns: 966.0
2
7
2
692
Returns: 692.0
5
4
5
945
Returns: 945.0
5
6
5
579
Returns: 579.0
1
8
1
577
Returns: 577.0
7
5
6
532
Returns: 1064.0
4
10
1
643
Returns: 3858.0
4
7
4
260
Returns: 260.0
10
9
3
455
Returns: 2730.0
1
3
1
77
Returns: 77.0
5
9
5
560
Returns: 560.0
8
7
6
941
Returns: 1882.0
1
10
1
921
Returns: 921.0
10
9
8
937
Returns: 1874.0
6
1
6
249
Returns: 249.0
4
6
4
982
Returns: 982.0
43
19
27
541
Returns: 1082.0
483
1
483
144
Returns: 144.0
63
127
5
856
Returns: 20544.0
26
2
13
127
Returns: 254.0
1
567
1
830
Returns: 830.0
218
268
15
551
Returns: 15428.0
4
9
1
433
Returns: 2598.0
616
638
59
345
Returns: 6900.0
195
43
5
473
Returns: 20339.0
14
9
5
972
Returns: 3888.0
12
628
1
624
Returns: 13728.0
223
13
25
605
Returns: 7865.0
11
37
5
604
Returns: 2416.0
14
736
2
831
Returns: 9972.0
1
15
1
334
Returns: 334.0
21
586
3
46
Returns: 552.0
432
91
22
988
Returns: 37544.0
818
279
197
416
Returns: 3328.0
2
7
1
22
Returns: 44.0
561
2
375
990
Returns: 1980.0
458
2
333
475
Returns: 950.0
6
14
1
130
Returns: 1300.0
4
28
4
605
Returns: 605.0
160
4
40
531
Returns: 2124.0
25
41
25
632
Returns: 632.0
1
915
1
360
Returns: 360.0
34
387
11
925
Returns: 5550.0
46
763
27
115
Returns: 230.0
21
138
3
335
Returns: 4020.0
304
22
15
567
Returns: 12474.0
768
968
21
904
Returns: 65088.0
431
304
3
628
Returns: 179608.0
89
7
13
109
Returns: 763.0
233
75
92
562
Returns: 2248.0
7
834
6
466
Returns: 932.0
79
6
14
469
Returns: 2814.0
838
94
12
712
Returns: 66928.0
107
42
3
476
Returns: 19992.0
118
6
117
10
Returns: 20.0
792
2
428
454
Returns: 908.0
47
3
47
283
Returns: 283.0
193
136
23
706
Returns: 11296.0
336
440
21
81
Returns: 2430.0
35
554
7
283
Returns: 2264.0
12
127
1
938
Returns: 20636.0
67
505
1
652
Returns: 86064.0
230
43
104
625
Returns: 2500.0
303
747
3
952
Returns: 190400.0
753
979
43
568
Returns: 19312.0
666
42
42
13
Returns: 390.0
10
3
9
1
Returns: 2.0
913
200
10
100
Returns: 18200.0
1000
1000
100
100
Returns: 1800.0
100
20
10
1
Returns: 18.0
10
3
5
1
Returns: 2.0
100
100
20
47
Returns: 376.0
2
1000
1
1000
Returns: 2000.0
8
100
2
8
Returns: 48.0
100
100
3
40
Returns: 2640.0
100
3
74
47
Returns: 94.0
200
3
100
47
Returns: 94.0
101
3
100
47
Returns: 94.0
100
1000
50
47
Returns: 94.0
101
100
20
47
Returns: 470.0
1000
1000
300
1
Returns: 6.0
100
13
20
47
Returns: 376.0
8
25
3
10
Returns: 40.0
100
100
20
100
Returns: 800.0
10
100
2
1
Returns: 8.0
201
1000
100
1
Returns: 4.0
100
23
70
49
Returns: 98.0
100
20
21
47
Returns: 376.0
3
10
1
1
Returns: 4.0
8
8
2
9
Returns: 54.0
120
30
7
10
Returns: 300.0
307
200
124
453
Returns: 1812.0
100
1000
10
47
Returns: 846.0
100
100
50
123
Returns: 246.0
101
11
20
47
Returns: 470.0
100
100
50
50
Returns: 100.0
10
100
9
3
Returns: 6.0
2
50
1
1
Returns: 2.0
100
11
20
47
Returns: 376.0
6
30
1
1
Returns: 10.0
2
3
1
1
Returns: 2.0
5
3
2
47
Returns: 141.0
10
9
2
1
Returns: 8.0
100
4
74
47
Returns: 94.0
19
100
9
3
Returns: 12.0
100
10
20
47
Returns: 376.0
901
1000
100
1
Returns: 18.0
41
11
20
47
Returns: 188.0
5
4
2
5
Returns: 20.0
1
3
1
13
Returns: 13.0
100
20
20
47
Returns: 376.0
850
10
170
849
Returns: 6792.0
20
100
2
2
Returns: 36.0
1000
1000
999
47
Returns: 94.0
30
100
10
1000
Returns: 4000.0
100
100
27
47
Returns: 282.0
20
3
10
10
Returns: 20.0
1000
80
200
51
Returns: 408.0
100
3
50
47
Returns: 94.0
102
5
50
40
Returns: 160.0
100
100
50
47
Returns: 94.0