cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2010 · MediumSimulation

Alternating Patrol Planes

Autumn Tournament (2010)

Submission name: planes.cpp

Problem summary

N equally spaced airports lie on a circle; adjacent airports are d units apart. Both planes start at airport 1, already visited by each. Plane 1 moves clockwise, plane 2 counterclockwise, alternating complete flights. On each flight a plane passes over its specified number of airports without landing, then lands at the first airport it has not previously visited. Count each plane’s total distance, meetings when a plane lands beside the other (including the initial meeting), and passes over the airport where the other plane is waiting. Stop when each has visited all airports.

Input

N d ap1 ap2.

Output

Distance of plane 1, distance of plane 2, number of meetings, and number of airborne passes, separated by spaces.

Constraints

2 ≤ N ≤ 1000; 1 ≤ d ≤ 100; 0 ≤ ap1, ap2 < N.

Worked example

Original statement sample, with whitespace restored where PDF extraction merges adjacent numbers.

Input

4 1 2 3

Output

9 15 4 3

Solution analysis

  1. Maintain a separate visited array and current airport for each plane. Initially both are at index zero and only that airport is marked in each array. There are N-1 rounds, each containing one flight by each plane.
  2. Simulate a flight one airport-to-airport segment at a time. The first ap airports reached must be passed over, even if unvisited. After those mandatory passes, continue past visited airports and land at the first unvisited one. Every segment adds d to that plane’s distance.
  3. At each arrival, first decide whether this is the landing. If the other plane is at this airport, increment meetings for a landing or passes for an overflight, never both. Airports skipped in the mandatory phase are not marked visited, since only landings count.
  4. For N=4, d=1, ap1=2 and ap2=3, plane 1 flies three segments on each of its three flights, for 9. Plane 2 flies five each time, for 15. They meet initially and after each plane-2 landing, giving four meetings, with three separate overflights of the waiting plane.

Why it works

Each simulated segment follows the specified direction. The landing test enforces the mandatory overflights followed by the first eligible airport. Separate landing histories reproduce both patrols, and each encounter is classified exactly once at the airport where it occurs.

Complexity: O(N²) time and O(N) space. There are 2(N-1) flights. A flight makes fewer than 2N segments: fewer than N mandatory passes and at most N more segments to find an unvisited airport.

Watch out. Do not mark overflown airports visited. Count every overflight, including repeated passages in one flight. An arrival that ends in landing is a meeting, not a pass. Include the initial meeting.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
#include<vector>
using namespace std;
int main(){
    int n;
    long long distanceBetween;
    int skip[2];
    cin>>n>>distanceBetween>>skip[0]>>skip[1];
    vector<vector<bool>>visited(2,vector<bool>(n,false));
    visited[0][0]=true;
    visited[1][0]=true;
    int position[2]={0,0};
    long long distance[2]={0,0};
    long long meetings=1;
    long long passes=0;
    for(int flight=1;flight<n;++flight){
        for(int plane=0;plane<2;++plane){
            int direction=(plane==0?1:-1);
            int arrivals=0;
            while(true){
                position[plane]=(position[plane]+direction+n)%n;
                distance[plane]+=distanceBetween;
                ++arrivals;
                // The first skip[plane] arrivals cannot end the flight.
                bool landing=arrivals>skip[plane]&&!visited[plane][position[plane]];
                if(position[plane]==position[1-plane]){
                    if(landing){
                        ++meetings;
                    }
                    else{
                        ++passes;
                    }
                }
                if(landing){
                    visited[plane][position[plane]]=true;
                    break;
                }
            }
        }
    }
    cout<<distance[0]<<' '<<distance[1]<<' '<<meetings<<' '<<passes<<'\n';
}

Source: Arena Olimpiici ↗