Skip to content

Instantly share code, notes, and snippets.

@IvanaGyro
Created April 25, 2019 12:30
Show Gist options
  • Save IvanaGyro/e67a616b654ce3a7fc33efcc3c665888 to your computer and use it in GitHub Desktop.
Save IvanaGyro/e67a616b654ce3a7fc33efcc3c665888 to your computer and use it in GitHub Desktop.
The wrong answer to the second problem of 2019 Kick Start round B
from operator import itemgetter
def eat(stones):
max_e = {}
stones.sort(key=itemgetter(2), reverse=True)
def dp(time, i):
if i == len(stones):
return 0
if (time, i) not in max_e:
max_e[(time, i)] = max(
dp(time, i + 1),
dp(time + stones[i][0], i + 1) + max(
stones[i][1] - time*stones[i][2],
0,
),
)
return max_e[(time, i)]
return dp(0, 0)
task = int(input())
for case in range(task):
n = int(input())
stones = [tuple(int(s) for s in input().split(' ')) for _ in range(n)]
ans = eat(stones)
print('Case: #{}: {}'.format(case, ans))
@dagolinuxoid
Copy link

I am not sure. Let's say you have all your stones in correctly sorted order. If you did it correctly, now in order to get result (sum up of total energy) I think you have to do something like this:

stones.forEach(([s,e,l])=> {
  let points = e-l*time;
  res+= (points<0 ? 0 : points);
  time+= s;
});

Sorry it's JS but I think it is readable? Does it make sense?
PS. I didn't figure out how to sort the stones in correct order yet, but once they are sorted correctly the code above is working.

@IvanaGyro
Copy link
Author

IvanaGyro commented Apr 25, 2019

I am able to write JS, so JS is fine. If the stones are sorted by L and E in descending, the logic of your method seems correct. But I still cannot pass the first test set after I changed the function, eat, to :

def eat(stones):
    max_e = {}
    stones.sort(key=lambda a: (a[2], a[1]), reverse=True)
    time = 0
    res = 0
    for stone in stones:
        res += max(0, stone[1] - time*stone[2])
        time += stone[0]
    return res

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment