#P8122. [BalticOI 2021] A Difficulty Choice (Day1)

[BalticOI 2021] A Difficulty Choice (Day1)

Description

You are determined to get accepted on BalticOI, and the way to do that is studying. You walk into a bookstore. There are NN books on the shelf, numbered from 11 to NN. The difficulty of the ii-th book is xix_i. You want to choose KK books out of these NN books for studying. You do not want the material to be too easy or too hard, so you want to ensure that the sum of difficulties of these KK books lies in the interval [A,2A][A,2A].

Unfortunately, you do not know the exact values of xix_i, so you need to skim the books to learn their difficulties. The shop owner is a neat freak and does not want you to skim too many books, so you are allowed to skim at most SS books and then decide. Luckily, you are told that as the book index increases, the difficulty is strictly increasing.

Please write a program that, by skimming books, buys the books you need, or reports that it is impossible.

Interaction Format

This is an interactive problem. You need to write the function void solve (int N, int K, long long A, int S). N,K,A,SN,K,A,S are defined above, and it is guaranteed that x1<x2<<xnx_1<x_2<\cdots<x_n. This function is called exactly once.

You may also call the following functions:

  • long long skim (int i) skims the ii-th book to obtain its difficulty xix_i.
  • void answer (vector<int> v) buys the books you need. Here v={i1,i2,,iK}v=\{i_1,i_2,\cdots,i_K\}, and it must satisfy:
Aj=1Kxij2AA \le \sum\limits_{j=1}^K x_{i_j} \le 2A
  • void impossible () reports that it is impossible to buy KK books as required.

If there exist KK books satisfying the requirement, you must call answer exactly once. Otherwise, you must call impossible exactly once. After that, the program stops automatically.

If your function calls do not follow the format above, or if you call skim more than SS times, the program stops automatically and the test will be judged as Not correct. You must not output anything to standard output, otherwise you will be judged as Security violation.

If you use C++, please include the books.h header file. If you want to check the correctness of your program, you can download sample_grader.cpp and books_sample.cpp from the attachments below; they are used for correctness checking and as an example, respectively.

If you use Python, you can download books_sample.py from the attachments below for testing.

The interactive library expects two lines on standard input:

  • The first line contains four integers, N,K,A,SN,K,A,S.
  • The second line contains NN integers, x1,x2,,xNx_1,x_2,\cdots,x_N.

Then the interactive library will call your program. Finally, the interactive library will output a message to standard output:

Message Meaning
Invalid input. The format of standard input is wrong
Invalid skim. The call to skim is invalid
Out of books to skim. skim is called more than SS times
Invalid answer. The call to answer is invalid
Wrong answer. The vector vv passed to answer does not satisfy the requirement
No answer. The solve function did not call either answer or impossible
Impossible (not checked): s book(s) skimmed. None of the above happened; skim was called SS times, and impossible was called when an answer exists
Correct: s book(s) skimmed. None of the above happened; skim was called SS times

For the error cases above, the interactive library will only return Not correct, or Correct when your solution is correct. Whenever any of the errors above occurs, or when your program calls answer or impossible, the program will be stopped automatically.

Input Format

See “Interaction Format”.

Output Format

See “Interaction Format”.

15 3 42 8

Hint

Sample 1 Explanation

N=15N=15, K=3K=3, A=42A=42, S=8S=8. Below are two possible sequences of calls that would be accepted:

Example 1:

Your program Return value
skim(1) 13371337
impossible -

Example 2:

Your program Return value
skim(1) 77
skim(15) 2121
answer({11,15,7}) -

Constraints and Assumptions

This problem uses bundled testdata.

  • Subtask 1 (5 pts): S=NS=N, 170N1000170 \le N \le 1000, K=3K=3.
  • Subtask 2 (15 pts): S=NS=N, N170N \ge 170.
  • Subtask 3 (10 pts): S170S \ge 170, xi+1xiAKx_{i+1}-x_i \le \frac A K.
  • Subtask 4 (15 pts): S170S \ge 170, xi+1xiAx_{i+1}-x_i \le A.
  • Subtask 5 (15 pts): S170S \ge 170.
  • Subtask 6 (20 pts): S40S \ge 40, xi+1xiAx_{i+1}-x_i \le A.
  • Subtask 7 (20 pts): S40S \ge 40.

For 100%100\% of the testdata, KNK \le N, 3N,S1053 \le N,S \le 10^5, 1A,xi10171 \le A,x_i \le 10^{17}, 3K103 \le K \le 10.

Note

Translated from BalticOI 2021 Day1 A A Difficulty Choice.

Translated by ChatGPT 5