cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2015 · EasyConservation of flow

Maya the Bee

Autumn Tournament (2015)

Submission name: bee.cpp

Problem summary

Three flowers stand at positions 0, 1 and 2 metres. They hold A, B and C bees, in nondecreasing order. It is possible to redistribute the bees equally. Find the minimum total distance flown by all bees.

Source clarification. Equalization is promised in the story, implicitly ensuring divisibility by 3. The sample explanation mistakenly says each flower ends with 3 bees; the correct equal count is 6.

Input

Three integers A, B, C.

Output

The minimum total distance in metres.

Constraints

0 ≤ A ≤ B ≤ C ≤ 100; A+B+C > 0. Equal redistribution is possible, so the sum is divisible by 3.

Worked example

Original statement sample.

Input

3 6 9

Output

6

Solution analysis

  1. Let target=(A+B+C)/3. Across the gap between the left flower and the other flowers, exactly target−A bees must move left. Across the other gap, exactly C−target bees must move left.
  2. Every crossing costs one metre. The minimum total distance is therefore (target−A)+(C−target). These flows can be realized by transferring surplus bees toward flowers that need them.
  3. For 3, 6, 9, the target is 6. Three bees travel from the right flower to the left flower, crossing both gaps, for 3·2=6 metres.

Why it works

Any valid redistribution must move target−A bees across the first gap and C−target across the second, so their sum is a lower bound. Move bees only leftward from surplus to deficit flowers; this realizes exactly those crossing counts without extra travel, attaining the bound.

Complexity: O(1) time and O(1) space.

Watch out. Count distance, not just moving bees: a right-to-left trip costs two metres.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    int target=(a+b+c)/3;
    // Each of the two one-metre gaps contributes its required flow.
    cout<<(target-a)+(c-target)<<'\n';
}

Source: Arena Olimpiici ↗