Informatics Olympiad all-in-one python version |

Learn Python from a baby! Use python to complete the topic of the Informatics Olympiad All-in-One website, and record every moment.

Attach a summary post: Informatics Olympiad All-in-One Python Edition |


【Description】

Pokemon is a story about the adventures of Ash and his partner Pikachu.

One day, Xiaozhi and Pikachu came to the elf hunting ground, where there are many precious wild Pokemon. Xiaozhi also wants to tame some of the elves. However, wild elves are not so easy to tame. For each wild elf, Xiaozhi may need to use many poke balls to subdue it, and in the process of subduing, the wild elves will also cause certain damage to Pikachu (thus reducing Pikachu's physical strength). When Pikachu's physical strength is less than or equal to 0, Xiaozhi must end the hunt (because he needs to heal Pikachu's injuries), and wild elves that make Pikachu's physical strength less than or equal to 0 will not be subdued by Xiaozhi. When Ash's Poké Balls run out, the hunt comes to an end.

Let's assume that Xiaozhi has two choices when he encounters a wild elf: subdue it, or leave it. If Xiaozhi chooses to subdue it, he will definitely throw a Poké Ball that can subdue the Pokémon, and Pikachu will definitely receive corresponding damage; if he chooses to leave it, then Xiaozhi will not lose the Poké Ball, and neither will Pikachu physical strength.

Xiaozhi has two goals: the main goal is to subdue as many wild elves as possible; if the number of elves that can be subdued is the same, Xiaozhi hopes that Pikachu will suffer less damage (greater remaining stamina), because they will continue adventure.

Now the number of Pokéballs of Ash and Pikachu's initial physical strength are known, the number of Pokéballs each Pokémon needs to be subdued and the amount of damage it will cause to Pikachu during the subduing process are known. May I ask, how does Xiaozhi choose which elves to subdue to achieve his goal?

【enter】

The first line of the input data contains three integers: N (0<N<1000), M (0<M<500), K (0<K<100), respectively representing the number of Ash’s Poké Balls, Pikachu’s initial Stamina, the number of wild elves.

In the following K lines, each line represents a wild elf, including two integers: the number of poke balls needed to tame the elf, and the damage caused to Pikachu during the taming process.

【Output】

The output is one line, containing two integers: C, R, respectively indicating that at most C elves can be subdued, and Pikachu's remaining physical strength is at most R when subduing C elves.

【Input sample】

10 100 5

7 10

2 40

2 50

1 20

4 20

【Example of output】

3 30

【Code Explanation】

N,M,K = [int(i) for i in input().split()] #N,M相当于二维背包中的a、b,相当于01背包中的M,K相当于01背包中的N

a = [0]
b = [0]

dp = [[0 for j in range(M+1)] for i in range(N+1)]

for i in range(1, K+1):
    ls = [int(i) for i in input().split()]
    a.append(ls[0])
    b.append(ls[1])

for i in range(1, K+1):
    for j in range(N, a[i]-1, -1):
        for l in range(M, b[i]-1, -1):
            dp[j][l] = max(dp[j][l], dp[j-a[i]][l-b[i]] + 1) #1为当前精灵数量

maxx = dp[N][M]

for i in range(1, M+1):
    if dp[N][i] == maxx:
        print(maxx, M-i)
        break

【operation result】

10 100 5
7 10
2 40
2 50
1 20
4 20
3 30

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130816553