This page is still under construction.

Parts of this page are still being built. What you see may change.

Hat Game

Time limit1.5sMemory limit128 MB

Summary
Devise a strategy for a line of N people (N ≤ 10) who each see the hats in front of them and say a number 0 to 63, maximizing correct guesses across T games.
Level

Medium6 of 10

Topics
Implementation, Bit manipulation, Combinatorics, Greedy
Solved
No attempts yet

Problem

Every Thursday, CS-House covers various stories about Yonsei University's Department of Computer Science in podcast form. On the episode aired March 18, 2021, alumnus Inseop Yoon, who had advanced to the ICPC World Final, appeared as a guest and talked about algorithms and competitive programming. During the conversation, he solved some fun problems together with the viewers, and the organizers decided to modify one of those problems and use it in the Yonsei University Freshman Programming Contest. Consider the following problem.

As shown in the figure, NN people stand in a line facing forward. Each person, except the one at the very back, wears a hat with an integer from 00 to 6363 written on it.

Each person can see the numbers on the hats of everyone in front of them, but cannot see the numbers on the hats of anyone behind them, including their own hat.

When the game starts, starting from the person at the very back, each person in order says one integer from 00 to 6363.

Before the game starts, the NN people gather to plan a strategy. Devise a strategy that maximizes the number of people whose spoken number equals the number on their own hat.

Implementation

To solve this problem, you must submit the file hat.cpp. The functions that must be included in hat.cpp are as follows.

void init(int N);
  • Called exactly once, immediately after the program starts running. N is the number of people standing in line.
int call(vector<int> F, vector<int> B, int num);
  • Returns the number that the (num+1)-th person from the front must say. num is an integer from 00 to N−1N-1.
  • F is an integer array of length NN holding the numbers on the hats worn by the people in front. F[i] holds the number on the hat worn by the (i+1)-th person. If the (num+1)-th person cannot see the number on the hat worn by the (i+1)-th person, that array value is 00.
  • B is an integer array of length NN holding the numbers spoken by the people behind. B[i] holds the number spoken by the (i+1)-th person. If the (i+1)-th person has not yet had a turn to speak before the (num+1)-th person speaks, that array value is 00.
  • In each game, call is called a total of NN times. On each call to call, num takes the values N−1N-1 down to 00 in order.

A total of TT hat games run simultaneously, and in each game N−1N-1 people must say the number equal to the one on their own hat to receive Accepted. If the grader judges the submission as wrong during execution, the program terminates immediately.

Constraints

  • 1≤N≤101 \le N \le 10
  • 1≤T≤200 0001 \le T \le 200\,000

Examples1

  1. Example 1

    Input
    1 1
    0
    
    Expected output
    0