60 likes | 205 Views
HKOI 2014 Junior. Magic Stone Problem Prepared By Sampson Lee. Problem. Given four operation Type Ti = 1: Buy Ai magic stones. Type Ti = 2: The system gives you Ai magic stones. Type Ti = 3: Dr. Jones uses Ai magic stones.
E N D
HKOI 2014 Junior Magic Stone Problem Prepared By Sampson Lee
Problem • Given four operation • Type Ti = 1: Buy Ai magic stones. • Type Ti = 2: The system gives you Ai magic stones. • Type Ti = 3: Dr. Jones uses Ai magic stones. • Type Ti = 4: Query: Among the magic stones Dr. Jones currently has, at most how many of them were bought? And at least how many of them were bought? • Simulate the above process
Observation • At most how many of them were bought? • If we always use the stones system gave us, it must be the best way to preserve the stones that were bought • At least how many of them were bought? • If we always use the stones that were bought, it must be the best way to preserve the stones that were given by the system
Solution • Maintain four variables, namely p, q, r, s • p : number of stones given by the system if we always use the stones given by the system first • q : number of stones bought if we always use the stones given by the system first • r: number of stones given by the system if we always use the stones we bought first • s : number of stones bought if we always use the stones we bought first
Solution • How to maintain? • Type Ti = 1: add Ai to q and s • Type Ti = 2: add Ai to p and r • Type Ti = 3: • Subtract Ai from p and s until there are no stone within, and subtract the residue stone from r and q • Type Ti = 4: Output q and r