编程介的小学生 2020-09-28 09:12 采纳率: 0.9%
浏览 15

Proud Merchants 代码的设计实现

Problem Description
Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.
If he had M units of money, what’s the maximum value iSea could get?

Input
There are several test cases in the input.

Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.

The input terminates by end of file marker.

Output
For each test case, output one integer, indicating maximum value iSea could get.

Sample Input
2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3

Sample Output
5
11

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-07-25 09:22
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    ```python def max_value(N, M, items):
    dp = [[0] * (M+1) for _ in range(N+1)]
    
    for i in range(1, N+1):
        for j in range(1, M+1):
            dp[i][j] = dp[i-1][j]
            if items[i-1][0] <= j and items[i-1][1] >= j:
                dp[i][j] = max(dp[i][j], dp[i-1][j-items[i-1][0]] + items[i-1][2])
    
    return dp[N][M]
    

    while True: try: N, M = map(int, input().split()) items = [] for _ in range(N): items.append(list(map(int, input().split())))

        result = max_value(N, M, items)
        print(result)
    except EOFError:
        break
    
    Explanation: 
    - The function `max_value` takes in the number of items `N`, initial money `M`, and a list of items `[Pi, Qi, Vi]` as parameters. It uses dynamic programming to find the maximum value iSea could get with the given constraints.
    - The main loop reads the input, processes it using the `max_value` function, and prints the result.
    - This code will handle multiple test cases until the end of the file is reached.
    - For the sample input provided, the output would be `5` and `11` respectively.
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵