DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

A Request to God for the Turn and River

Back to problem

Editorial

The four hole cards and three flop cards already occupy 777 cards, so exactly 52−7=4552-7=4552−7=45 cards are available for the turn or river. Since the two positions are ordered and must use different physical cards, there are only

45×44=198045\times 44=198045×44=1980

possible turn-river pairs. We can check all of them directly.

For each pair, evaluate the final seven-card hand of each player. A poker hand uses five of those seven cards, so enumerate all

(75)=21\binom{7}{5}=21(57​)=21

five-card subsets and take the strongest one.

A five-card hand can be encoded as an integer tuple

(category, first tiebreak value, second tiebreak value,…).(\text{category},\ \text{first tiebreak value},\ \text{second tiebreak value},\ldots).(category, first tiebreak value, second tiebreak value,…).

Give stronger categories larger category numbers, and store the remaining values in the exact tiebreak order used by Texas Hold'em. Then ordinary lexicographic comparison of the tuples determines which hand is stronger. For example, four of a kind can be represented as

(7, four-of-a-kind rank, kicker, 0, 0, 0).(7,\ \text{four-of-a-kind rank},\ \text{kicker},\ 0,\ 0,\ 0).(7, four-of-a-kind rank, kicker, 0, 0, 0).

When detecting straights, handle both ordinary Ace-high usage and the five-high wheel A2345. Fill the comparison tuple similarly for flushes, full houses, three of a kind, two pair, one pair, and high-card hands.

If the chosen turn is exactly the originally scheduled turn card, its cost is 000; otherwise pay the cost of the chosen card's rank. The river is handled in the same way.

While scanning every turn-river pair, maintain the minimum cost among winning outcomes and the minimum cost among draws separately. If any winning outcome exists, output a minimum-cost winning choice. Otherwise output a minimum-cost draw if one exists. If neither exists, output -1.

The amount of work per test case is bounded by a fixed constant, so the time and extra space complexities per case are O(1)O(1)O(1). Over the whole input, the running time is O(T)O(T)O(T).

Solution written by GPT5.6