Merge pull request '2508/808' (#2) from 2508/808 into main
Reviewed-on: #2
This commit is contained in:
24
202508/808 soupServings/C#/C#.sln
Normal file
24
202508/808 soupServings/C#/C#.sln
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.5.2.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "soupServings", "soupServings.csproj", "{6D960F00-6A53-6E19-86A1-4BF3D1CD764F}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{6D960F00-6A53-6E19-86A1-4BF3D1CD764F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{6D960F00-6A53-6E19-86A1-4BF3D1CD764F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{6D960F00-6A53-6E19-86A1-4BF3D1CD764F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{6D960F00-6A53-6E19-86A1-4BF3D1CD764F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {11CC27B7-C17E-4820-8A68-ADD32A2B37F8}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
56
202508/808 soupServings/C#/Program.cs
Normal file
56
202508/808 soupServings/C#/Program.cs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
private Dictionary<(int, int), double> dp = new Dictionary<(int, int), double>();
|
||||||
|
public double SoupServings(int n)
|
||||||
|
{
|
||||||
|
if (n > 5000)
|
||||||
|
return 1.0;
|
||||||
|
|
||||||
|
n = (n + 24) / 25;
|
||||||
|
|
||||||
|
// use dynamic programming
|
||||||
|
return Helper(n, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
private double Helper(int a, int b)
|
||||||
|
{
|
||||||
|
if (a <= 0 && b <= 0)
|
||||||
|
{
|
||||||
|
return 0.5;
|
||||||
|
}
|
||||||
|
if (a <= 0)
|
||||||
|
{
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
if (b <= 0)
|
||||||
|
{
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
if (dp.ContainsKey((a, b)))
|
||||||
|
{
|
||||||
|
return dp[(a, b)];
|
||||||
|
}
|
||||||
|
|
||||||
|
double probability = 0.25 * (
|
||||||
|
Helper(a - 4, b) +
|
||||||
|
Helper(a - 3, b - 1) +
|
||||||
|
Helper(a - 2, b - 2) +
|
||||||
|
Helper(a - 1, b - 3)
|
||||||
|
);
|
||||||
|
|
||||||
|
dp[(a, b)] = probability;
|
||||||
|
return probability;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main()
|
||||||
|
{
|
||||||
|
var Solution = new Solution();
|
||||||
|
var ans = Solution.SoupServings(800);
|
||||||
|
Console.WriteLine(ans);
|
||||||
|
}
|
||||||
|
}
|
10
202508/808 soupServings/C#/soupServings.csproj
Normal file
10
202508/808 soupServings/C#/soupServings.csproj
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
52
202508/808 soupServings/Go/main.go
Normal file
52
202508/808 soupServings/Go/main.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import ("fmt")
|
||||||
|
|
||||||
|
type Solution struct{
|
||||||
|
dp map[[2]int]float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSolution() *Solution{
|
||||||
|
return &Solution{dp: make(map[[2]int]float64)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Solution) SoupServings(n int)float64{
|
||||||
|
if n > 5000{
|
||||||
|
return 1.0
|
||||||
|
}
|
||||||
|
|
||||||
|
n = (n+24) /25
|
||||||
|
|
||||||
|
return s.helper(n, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s * Solution) helper(a, b int) float64 {
|
||||||
|
if a <= 0 && b <= 0{
|
||||||
|
return 0.5
|
||||||
|
}
|
||||||
|
if a <= 0{
|
||||||
|
return 1.0
|
||||||
|
}
|
||||||
|
if b <= 0{
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if val, exist := s.dp[[2]int{a, b}]; exist{
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
probability := 0.25 * (
|
||||||
|
s.helper(a-4, b) +
|
||||||
|
s.helper(a-3, b-1) +
|
||||||
|
s.helper(a-2, b-2) +
|
||||||
|
s.helper(a-1, b-3))
|
||||||
|
|
||||||
|
s.dp[[2]int {a, b}] = probability
|
||||||
|
return probability
|
||||||
|
}
|
||||||
|
|
||||||
|
func main(){
|
||||||
|
Solution := NewSolution()
|
||||||
|
ans := Solution.SoupServings(100)
|
||||||
|
fmt.Println(ans)
|
||||||
|
}
|
14
202508/808 soupServings/description.md
Normal file
14
202508/808 soupServings/description.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
You have two soups, A and B, each starting with n mL. On every turn, one of the following four serving operations is chosen at random, each with probability 0.25 independent of all previous turns:
|
||||||
|
|
||||||
|
pour 100 mL from type A and 0 mL from type B
|
||||||
|
pour 75 mL from type A and 25 mL from type B
|
||||||
|
pour 50 mL from type A and 50 mL from type B
|
||||||
|
pour 25 mL from type A and 75 mL from type B
|
||||||
|
Note:
|
||||||
|
|
||||||
|
There is no operation that pours 0 mL from A and 100 mL from B.
|
||||||
|
The amounts from A and B are poured simultaneously during the turn.
|
||||||
|
If an operation asks you to pour more than you have left of a soup, pour all that remains of that soup.
|
||||||
|
The process stops immediately after any turn in which one of the soups is used up.
|
||||||
|
|
||||||
|
Return the probability that A is used up before B, plus half the probability that both soups are used up in the same turn. Answers within 10-5 of the actual answer will be accepted.
|
Reference in New Issue
Block a user