#P6716. [CCO 2018] Gradient Descent

[CCO 2018] Gradient Descent

Description

Troy wants to play a game with you. The game uses an R×CR \times C grid.

There are NN pieces (numbered from 11 to NN). The ii-th piece is placed at (Xi,Yi)(X_i, Y_i). Multiple pieces may be on the same cell.

In one second, Troy can move one piece to a vertically or horizontally adjacent cell. Troy defines the score of a cell as the minimum number of seconds needed to move all NN pieces to that cell. Your task is to find the minimum score among all cells on the board.

Troy will not tell you the value of NN or the positions of the pieces, but you may ask Troy at most KK questions: the score of cell (p,q)(p, q).

Interactive Strategy

This is an interactive problem.

First, read three integers R,C,KR, C, K, representing the size of the grid and the maximum number of questions you may ask.

After reading this line, your program may ask questions about the score of (p,q)(p, q) (in the format ? p q), and then you will receive an integer ss representing the score of that cell.

Once your program determines the minimum score among all cells on the board, it should output ! Z and terminate, where ZZ is the final answer.

After outputting each line, you must flush the buffer:

  • C/C++: fflush(stdout) or cout << endl
  • Java: System.out.flush()
  • Pascal: flush(output)

If your output format is incorrect, or you ask more than KK questions, your answer will be judged incorrect.

Input Format

None

Output Format

None

1 10 90 3
1 2
1 4
1 10


5 4 170 4
2 3
2 3
4 3
5 1


Hint

Sample Explanation

For sample 11

Request Response
1 10 90
? 1 3 9
? 1 7 11
? 1 4 8
! 8

The piece positions should be (1,2)(1,2), (1,4)(1,4), and (1,10)(1,10), so the minimum score over the whole board is 88 (the score of cell (1,4)(1,4)).

For sample 22

Request Response
5 4 170
? 2 4 11
? 1 4 15
? 3 3 7
! 7

The piece positions should be (2,3)(2,3), (2,3)(2,3), (4,3)(4,3), (5,1)(5,1).

Constraints

For 100%100\% of the testdata, 1R,C1071 \le R, C \le 10^7, 1K1701 \le K \le 170, 1pR1 \le p \le R, 1qC1 \le q \le C, 0s2×1090 \le s \le 2 \times 10^9, 1N1001 \le N \le 100, 1XiR1 \le X_i \le R, 1YiC1 \le Y_i \le C
For 20%20\% of the testdata, R=1R = 1, C90C \le 90, K=90K = 90
For another 20%20\% of the testdata, R=1R = 1, K=90K = 90
For another 20%20\% of the testdata, K=170K = 170
For another 20%20\% of the testdata, K=100K = 100
For another 20%20\% of the testdata, K=75K = 75

The spj and interactive library are included in the attached files.

Notes

Translated from Canadian Computing Olympiad 2018 Day 2 A Gradient Descent

spj author:

https://www.luogu.com.cn/user/174045

Translated by ChatGPT 5