Very Frequent
0/15

# Simulation

Author: Darren Yao

Contributors: Allen Li, Siyong Huang, Juheon Rhee

Directly simulating the problem statement.

Example 1SolutionExample 2SolutionProblemsEasierHarder
Resources
IUSACO

This module is based on Chapter 5 of Darren Yao's book

Since there's no formal algorithm involved, the intent of the problem is to assess competence with one's programming language of choice and knowledge of built-in data structures. At least in USACO Bronze, when a problem statement says to find the end result of some process, or to find when something occurs, it's usually sufficient to simulate the process naively.

## Example 1

Focus Problem – try your best to solve this problem before continuing!

### Solution

We can simulate the process. Store an array that keeps track of which shell is at which location, and Bessie's swapping can be simulated by swapping elements in the array. Then, we can count how many times Elsie guesses each shell, and the maximum points she can earn is the maximum amount of times a shell is guessed.

C++

#include <algorithm>#include <cstdio>#include <vector>
using std::vector;
int main() {	freopen("shell.in", "r", stdin);
int n;

Java

import java.io.*;import java.util.*;
public class Shell {	public static void main(String[] args) throws IOException {		Kattio io = new Kattio("shell");
int n = io.nextInt();
// shellAtPos[i] stores the label of the shell located at position i

Python

read = open("shell.in")
# shell_at_pos[i] stores the label of the shell located at position i# The shells can be placed arbitrarily at the start.shell_at_pos = [i for i in range(3)]
# counter[i] stores the number of times the shell with label i was pickedcounter = [0 for _ in range(3)]

## Example 2

Focus Problem – try your best to solve this problem before continuing!

### Solution

We can simulate the process of pouring buckets. The amount of milk poured from bucket $i$ to bucket $j$ is the smaller of the amount of milk in bucket $i$ (which is $m_i$) and the remaining space in bucket $j$ (which is $c_j - m_j$). We can just handle all of these operations in order, using an array $c$ to store the maximum capacities of each bucket, and an array $m$ to store the current milk level in each bucket, which we update during the process. Example code is below.

C++

#include <algorithm>#include <cstdio>#include <iostream>#include <vector>
using namespace std;
const int N = 3;  // The number of buckets (which is 3)const int TURN_NUM = 100;


Java

import java.io.*;import java.util.*;
public class MixMilk {	static final int N = 3;  // The number of buckets (which is 3)	static final int TURN_NUM = 100;
public static void main(String[] args) throws IOException {		Kattio io = new Kattio("mixmilk");


Python

N = 3  # The number of buckets (which is 3)TURN_NUM = 100
# capacity[i] is the maximum capacity of bucket icapacity = [0 for _ in range(N)]# milk[i] is the current amount of milk in bucket imilk = [0 for _ in range(N)]with open("mixmilk.in") as read:	for i in range(N):		capacity[i], milk[i] = map(int, read.readline().split())

## Problems

### Easier

StatusSourceProblem NameDifficultyTags
BronzeEasy
Show TagsSimulation
BronzeEasy
Show TagsSimulation
BronzeEasy
Show TagsSimulation
BronzeEasy
Show TagsSimulation
BronzeEasy
Show TagsSimulation

### Harder

StatusSourceProblem NameDifficultyTags
BronzeNormal
Show TagsSimulation
BronzeNormal
Show TagsSimulation
BronzeNormal
Show TagsSimulation
BronzeNormal
Show TagsSimulation
BronzeNormal
Show TagsSimulation
Old BronzeHard
Show TagsSimulation
BronzeHard
Show TagsSimulation
BronzeVery Hard
Show TagsSimulation

### Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!