cppAcademy
FROM LEARNING TO COMPETING

Real problems.
Your next challenge.

Explore the Group C Olympiad archive.
Choose a topic. Find a challenge. Build your solution.

213 tasks across the Municipal, Regional, and National rounds (2009–2026), plus 4 previously featured problems. 146 tasks have independently written English summaries, analyses, and C++17 solutions; the remaining entries link to Arena. Topic tags are from Arena or our reviewed explanations. Difficulty labels are learning estimates, not official ratings.
GROUP C2017 · MediumCounting

Classroom Greetings

National Olympiad — Municipal round

Submission name: klas.cpp

Problem summary

Students enter in the supplied order. An arriving boy shakes hands with every boy already inside and exchanges one greeting in each direction with every girl. An arriving girl says one greeting to the whole room if it is nonempty; each student already inside replies once. Count handshakes and individual spoken greetings.

Input

A nonempty string: + denotes a girl and − (ASCII minus) a boy.

Output

Handshakes and spoken greetings, separated by a space.

Constraints

1 ≤ string length ≤ 20000.

Worked example

An independently prepared example for this explanation.

Input

--+

Output

1 3

Solution analysis

  1. Track the number of boys, girls and students already present.
  2. A boy adds boys handshakes and twice girls greetings.
  3. A girl entering a nonempty room adds one plus the previous number of students greetings.

Why it works

Each greeting and handshake is assigned uniquely to the arriving student who triggers it.

Complexity: O(N) time; O(1) auxiliary space.

Watch out. A girl’s initial greeting is a single utterance, not one per classmate.

Commented C++17 solution

An independently written complete program with standard headers.

// Key idea: Each greeting and handshake is assigned uniquely to the arriving student who triggers it.
#include<iostream>
#include<string>
using namespace std;
int main(){
    string s;
    cin>>s;
    long long b=0,g=0,h=0,w=0;
    for(char c:s){
        if(c=='-'){
            h+=b;
            w+=2*g;
            b++;
        }
        else{
            if(b+g){
                w+=1+b+g;
            }
            g++;
        }
    }
    cout<<h<<' '<<w<<'\n';
}

Source: Arena Olimpiici ↗