hdu3466 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

要排序大家都知道,我就分享一下我是怎么证明为什么要这么排序的,假设两个东西A(p1,q1),B(p2,q2)。

第一次更新dp值后(q1,m)的范围被更新,(p1,q1)未被更新。那么第二次更新时,要更新的值是(q2,m)的范围,设(q2,m)内一个数为x  ,那么只要x∈(q2,m),x-p2不经过(p1,q1)这一段就行了,那么x-p2大于q1就完事了。所以需要x-p2>q1>q1-p1;所以需要x>q1-p1+p2;

 

又由于x>q2;

所以只要q2>q1-p1+p2;

得q2-p2>q1-p1;证毕;

附巨丑图一张。

 

网上的找了半天看的头都大,所以想了个水的。(是不是最简单的我也不知道,标题先吹了在说)。

// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

//#include "pch.h"
#include <iostream>
#include<cstring>
#include<algorithm>

using namespace std;
const int  inf = 0x3f3f3f3f;
int dp[5005];
struct node{
	int q, p, v;
}a[505];
bool cmp(node b,node c) {
	return b.q - b.p < c.q - c.p;
}
int main()
{
	int n, m;
	while (cin >> n >> m) {
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; i++) {
			cin >> a[i].p >> a[i].q >> a[i].v;
		}
		sort(a + 1, a + 1 + n,cmp);
		for (int i = 1; i <= n; i++) {
			for (int j = m; j >=a[i].q; j--) {
					dp[j] = max(dp[j], dp[j - a[i].p] + a[i].v);
			}
		}
		cout << dp[m] << endl;
	}
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

 

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值