Two related settings: interactive problems, where you construct the answer by asking questions; and output-only or “any valid answer” problems, where a randomized or search-based construction plus a checker is acceptable.
| Setting | Technique |
|---|---|
| find a hidden object with few queries | each query should split the remaining candidates (information bound ) |
| comparisons only | tournament for the max (), max and min together () |
| hidden permutation, pairwise queries | find values by pairs, keep one “unknown” candidate |
| adaptive judge | your strategy must work for every consistent answer: think of the judge as an adversary |
| output-only, large instances | local search, simulated annealing, randomized restarts, then verify |
| ”any valid answer” with a random construction | random choice + checker in a loop; expected tries |
Hidden permutation with mod queries
Query returns . Two queries on a pair reveal the smaller value: if , then while .
// hidden permutation, query(i, j) returns p_i mod p_j; 2 queries per step find a value
template<class Q> vector<int> guessPermutation(int n, Q query) {
vector<int> p(n, 0); int cand = 0; // cand: index whose value is still unknown (the current maximum candidate)
for (int i = 1; i < n; i++) {
int x = query(cand, i), y = query(i, cand);
if (x > y) p[cand] = x, cand = i; // p_cand < p_i, so p_cand mod p_i = p_cand
else p[i] = y;
}
p[cand] = n;
return p;
}Proof: 2(n−1) queries recover the permutation
For distinct : and . So of the two answers the larger one is , and it belongs to the index with the smaller value. Each step therefore determines one value and keeps the larger element as the candidate. After steps every value except the maximum is known, and the maximum is . Tested with a local judge on random permutations up to , counting queries.
See Interactive Problems for I/O, flushing and local judges, and Heuristic Optimization for output-only search.