python - What's incorrect about my sollution in this google code jam exercise? -
today, i've participated in google code jam round 1b. in code jam, there problem called 'osmos': https://code.google.com/codejam/contest/2434486/dashboard
problem description
the problem states there's game, player thing, can eat things smaller it, , become larger size of thing ate. example, if player has size 10 , eats of size 8, becomes size 18.
now, given size player starts off at, , sizes of other things in game, should give minimum number of operations required make game beatable, means you're able eat everything.
an operation can either adding thing, or removing thing.
the code used
write_case
function writes output every test case in right format. i've used in other code jam rounds, , know correct, , inp
file object of input file.
cases = int(inp.readline()) case in xrange(cases): # armin variable containing size of player, armin, n = int(inp.readline.split()[0]) # others list of sizes of other things others = [int(x) x in inp.readline().split()] # changes holds number of required changes changes = 0 other in sorted(others): #we loop on other things in order. if other < armin: #if other thing smaller, eat it, no change needed. armin += other else: # we'll need make changes # adding biggest size thing player can eat, adding = armin - 1 if other < armin + adding: #if adding such thing enough # eat other thing armin += adding + other # add , eat them changes += 1 #we've made change. else: # can't add big enough thing # have elete game (skip loop) # change changes += 1 write_case(case + 1, changes ) #output changes.
my logic behind it
by looping on te other things froom small large, player first eats can eat. when encounter can't eat, we've eaten that's smaller it, we'll have add new thing can grow. if we're adding new, might make big possible, since size of thing add doesn't change number of operations. largest eatable thing can add, player's size -1. if that's enough eat next thing, add it, eat thing added, , eat thing couldn't eat.
if addition wouldn't enough, don't add it, , delete (ignore) current thing). @ point, break loop skip other things (since the'll large eat, list sorted.), add number number of operationns speed sollution, wouldn't change outcome.
this code correctly computes values sample input, it's incorrect contest input. idea why?
my approach each time found block, figured out how many additions required continue. built log of number of additions , number of remaining. after had completed set, looped through log in reverse determine if more efficient add motes continue, or remove remaining motes @ each block point. looking @ now, can see number of places optimize, passed both small , large below c# code.
protected string solve(string line1, string line2) { string[] inputs = line1.split(); uint = uint.parse(inputs[0]); byte n = byte.parse(inputs[1]); inputs = line2.split(); list<uint> motes = new list<uint>(n); foreach (string size in inputs) { motes.add(uint.parse(size)); } motes.sort(); list<action> actions = new list<action>(); while (motes.count > 0) { if (a > motes[0]) { += motes[0]; motes.removeat(0); } else if(a > 1) { uint i; (i = 0; <= motes[0]; i++) { = (a << 1) - 1; } actions.add(new action(i, motes.count)); } else { actions.add(new action(101, motes.count)); break; } } uint totalinserts = 0; int totalremoved = 0; (int = actions.count - 1; >= 0; i--) { int stepremaining = actions[i].remaining - totalremoved; uint stepinsert = actions[i].inserts; if (stepinsert >= stepremaining) { totalremoved += stepremaining; totalinserts = 0; } else { totalinserts += stepinsert; if (totalinserts >= actions[i].remaining) { totalremoved = actions[i].remaining; totalinserts = 0; } } } return (totalinserts + totalremoved).tostring(); } struct action { public uint inserts; public int remaining; public action(uint inserts, int remaining) { inserts = inserts; remaining = remaining; } }
Comments
Post a Comment