Very Frequent
 0/15

Simulation

Author: Darren Yao

Contributors: Allen Li, Siyong Huang, Juheon Rhee

Directly simulating the problem statement.

Edit This Page
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")
n = int(read.readline())
# 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 picked
counter = [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 ii to bucket jj is the smaller of the amount of milk in bucket ii (which is mim_i) and the remaining space in bucket jj (which is cjmjc_j - m_j). We can just handle all of these operations in order, using an array cc to store the maximum capacities of each bucket, and an array mm 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 i
capacity = [0 for _ in range(N)]
# milk[i] is the current amount of milk in bucket i
milk = [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

Module Progress:

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!