QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100 Interactive

#1399. Ramen

Statistics

JOI-kun and IOI-chan love ramen. JOI-kun likes light ramen, while IOI-chan likes rich ramen. In the town where JOI-kun and IOI-chan live, there are $N$ ramen shops, numbered from $0$ to $N-1$.

It is unknown which ramen shops serve rich ramen and which serve light ramen. Therefore, JOI-kun and IOI-chan decided to visit nearby ramen shops to determine which shop serves the lightest ramen and which shop serves the richest ramen.

Each ramen shop in the town has a specific "richness level" for the ramen served there. The richness levels are integers between $0$ and $N-1$, and the richness level of each ramen shop is unique. JOI-kun and IOI-chan can visit two ramen shops in one day and compare the tastes to determine which of the two shops has a higher richness level.

For health reasons, JOI-kun and IOI-chan want to limit the number of days they spend comparing ramen to at most 600 days.

Task

Given the number of ramen shops $N$ in the town, write a program that determines the ramen shop with the lowest richness level and the ramen shop with the highest richness level among the $N$ shops using at most 600 comparisons.

Implementation Details

You must write a program that implements the method to determine the ramen shop with the lowest richness level and the ramen shop with the highest richness level using at most 600 comparisons.

Your program must implement the following routine:

  • void Ramen(int N)

    This routine is called exactly once for each test case. The argument $N$ is the number of ramen shops in the town.

    This routine must determine the ramen shop with the lowest richness level and the ramen shop with the highest richness level by calling Compare, and must terminate by calling Answer.

You can call the following functions in your program:

  • int Compare(int X, int Y)

    This function is called when comparing ramen tastes. The arguments $X$ and $Y$ are the indices of the ramen shops to be compared. $X$ and $Y$ must be distinct integers between $0$ and $N-1$. If Compare is called with arguments that do not satisfy this, it will result in Wrong Answer [1] and the program will terminate.

    This function returns $1$ if the richness level of ramen shop $X$ is greater than that of ramen shop $Y$, and returns $-1$ if the richness level of ramen shop $X$ is less than that of ramen shop $Y$.

    If Compare is called more than 600 times, it will result in Wrong Answer [2] and the program will terminate.

    Ramen must terminate by calling the following routine. If Ramen does not call Answer, it will result in Wrong Answer [3] and the program will terminate.

  • void Answer(int X, int Y)

    This routine is called when the ramen shop with the lowest richness level and the ramen shop with the highest richness level have been determined. The arguments $X$ and $Y$ are the index of the ramen shop with the lowest richness level and the index of the ramen shop with the highest richness level, respectively. $X$ and $Y$ must be integers between $0$ and $N-1$. If Answer is called with arguments that do not satisfy this, it will result in Wrong Answer [4].

    If the answer is uniquely determined by the results of the Compare calls and you call Answer with that answer, it is considered Correct. Otherwise, it is Wrong Answer [5].

    When this routine is called, the program terminates.

Compilation and Execution

A sample grader is provided in the archive that can be downloaded from the contest site. This archive also contains a sample of the file you must submit.

The sample grader consists of a single file, grader.c or grader.cpp. For example, if your program is Ramen.c or Ramen.cpp, you can test your program by executing the following commands:

  • For C: gcc -O2 -lm grader.c Ramen.c -o grader

  • For C++: g++ -O2 grader.cpp Ramen.cpp -o grader

If compilation succeeds, an executable file named grader is generated.

Note that the actual grader used for evaluation is different from the sample grader. The sample grader runs as a single process. It reads input from standard input and writes the result to standard output.

Sample Grader Input

The sample grader reads the following input from standard input:

  • The first line contains two integers $N$ and $T$ separated by a space. $N$ is the number of ramen shops. The sample grader handles data where $T = 1$.

  • The following $N$ lines, where the $(i+1)$-th line ($0 \le i \le N-1$) contains an integer $A_i$ ($0 \le A_i \le N-1$). This represents the richness level of ramen shop $i$.

Sample Grader Output

If the program terminates normally, the sample grader outputs the following information to standard output in a single line:

  • If the answer is correct, "Accepted" is output.

  • If the answer is incorrect, the type of error is output, such as "Wrong Answer [2]".

Note that the sample grader will judge the answer as correct even if it corresponds to Wrong Answer [5] in the case where $A_X = 0$ and $A_Y = N-1$. Be aware that this behavior differs from the actual grader.

Note

During grading, if the answer is not uniquely determined by the results of the Compare calls, it will be judged as Wrong Answer [5], regardless of the arguments passed to Answer.

In some test data, the return value of Compare may change depending on the content of previous Compare calls. Even in this case, the return value of Compare will not contradict the results of previous Compare calls.

Constraints

All input data satisfies the following conditions:

  • $1 \le N \le 400$.

Subtasks

Subtask 1 [20 points]

  • $N \le 30$ is satisfied.

Subtask 2 [30 points]

  • $N \le 300$ is satisfied.

Subtask 3 [50 points]

  • No additional constraints.

Examples

Input 1

3 1
1
2
0

Output 1

Compare(0, 1) returns -1
Compare(0, 2) returns 1
Answer(2, 1) program terminates

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.