130 likes | 333 Views
Dice. HKOI 2012 Tony Wong. Hi!. Tony Wong Year 4, Dual Degree Program in Technology and Management, HKUST (JA5309) BEng (Computer Engineering) & BBA (General Business Management). Problem Description . Dr. Jones wants to cheat in the game of Cheating Dice He knows all the dice values
E N D
Dice HKOI 2012 Tony Wong
Hi! • Tony Wong • Year 4, Dual Degree Program in Technology and Management, HKUST(JA5309) • BEng (Computer Engineering) &BBA (General Business Management)
Problem Description • Dr. Jones wants to cheat in the game of Cheating Dice • He knows all the dice values • Given last guess, determine the lowest point value guess that Dr. Jones can make
Problem Description • Rule 1: Players may only make guesses which worth more points (6c+v) than the last players' • Rule 2: The total number of dice rolled 1 or v must be at least c. (If v=1, count only the dice rolled 1)
Disclaimer • Do not follow these rules in real life
Example N = 2 Rule 1 Rule 2
Constraints • In test cases worth 30% • N = 2 • In test cases worth 50% • dice values are from 2 to 6 inclusive • No need to add 1s • In all test cases • 2 ≤N ≤10000, 2 ≤ V ≤ 6 • No need to consider the effect of 1s after 1 is guessed • It is guaranteed that there are at least one valid guess.
Solution 1: Loop the answer • For c=1 to 5N • For v=1 to 6 • For each dice if it is 1 or v, sum++; • If sum≥cAND 6c+v is large enough, output (c, v) • Time complexity: O(30N2)
Solution 1a: Loop the answer • For c=1 to 5N • For v=1 to 6 • If 6c+v is large enough, • For each dice if it is 1 or v, sum++; • If sum ≥ c, output (c, v) • Time complexity: O(6N) • Why?
Solution 2: Pre-calculation • For each dice • If it is 1, s[2]++; s[3]++; s[4]++; s[5]++; s[6]++; • s[v]++; • For c=1 to 5N • For v=1 to 6 • If 6c+v is large enough, • If s[v]≥ c, output (c, v) --- O(1) • Time complexity: O(5+1N)
Solution 2: Pre-calculation • For each dice • If it is 1, s[2]++; s[3]++; s[4]++; s[5]++; s[6]++; • s[v]++; • While (true) { • If 6c+v is large enough, • If s[v]≥ c, output (c, v) --- O(1) • v++; if (v==7) { v=1; c++; } • } • Time complexity: O(6+N)