uva10154 - Weights and Measures(01背包)

题目:uva10154 - Weights and Measures(01背包)

题目大意:给出一些乌龟的重量和力量,问怎样将这些乌龟一只放一只背上的堆起来,要求这样序列的乌龟越多越好,并且不要超出每只乌龟的负荷。

解题思路:一开始,以为只要将乌龟的力量从大到小的排下序,然后根据力量找出最长的序列。结果发现这样的做法是不对的,因为这样你就默认了越前面的乌龟就放越底下,(但是其实是可以不用的)虽说前面的乌龟力量比较大,但是重量可能也比较大,这样承受力就会变小。后来看了大神的做法,01背包。这些乌龟要么取要么不取,然后根据(力量 - 重量)承受力DP。因为承受力是逐步减少的,所以还是贪心一下,把力量大的放在前面取。

dp【i】【load】 = Max (dp【i - 1】【j】 + 1, dp【i - 1】【load】) load = Min(j - W【i】, S[i] - W[i]) 。当dp【i  - 1】【j】有值的时候才用。  因为这里数据大,要用滚动数组。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 5610;

struct turtle {

	int w, s;
}t[N];

int f[N * N];

int cmp (const turtle &a, const turtle &b) {

	if (a.s - a.w == b.s - b.w)
		return a.w < b.w;
	return (a.s - a.w) > (b.s - b.w);
}

int Min (const int a, const int b ) { return a < b ? a: b; }

int main () {

	int n = 0;
	while (scanf ("%d%d", &t[n].w, &t[n].s) != EOF) {

		n++;
	}

	sort(t, t + n, cmp);

	int max = t[0].s - t[0].w;
	int tmp;
	for (int i = 0; i < n; i++) {
		for (int j = t[i].w; j <= max; j++) {

			if (f[j]) {

				tmp = Min (j, t[i].s) - t[i].w;
				if (f[tmp] < f[j] + 1)
					f[tmp] = f[j] + 1;
			}
		}
		tmp = t[i].s - t[i].w;
		if (!f[tmp])
			f[tmp] = 1;
	}

	int ans = 0;
	for (int i = 0; i <= max; i++)
		if (ans < f[i])
			ans = f[i];
	printf ("%d\n", ans);
	return 0;
}
时间: 2024-10-12 14:30:01

uva10154 - Weights and Measures(01背包)的相关文章

UVA10154 Weights and Measures

https://vjudge.net/problem/UVA-10154 ↑Vjudge大法好 堆一个乌龟塔.每只乌龟有重量w和承重能力s(也要承受自己的重量,所以实际可托起s-w),问最多能堆几只乌龟 很明显是DP 从低往高堆不太好判断,因为不知道下面的乌龟还能不能承受得了. 切换到上帝视角,对于一个乌龟塔,我们可以直接把它托起来,然后在塔下面塞一只能承重的乌龟. 问题解决了. 1 #include<iostream> 2 #include<cstring> 3 #include

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

01背包专题

>>什么是01背包<< A - Bone Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to co

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

POJ 3624 Charm Bracelet (01背包)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26078   Accepted: 11726 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

Charm Bracelet-POJ3624(01背包)

http://poj.org/problem?id=3624 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29444   Accepted: 13198 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it wi

转化一下就是01背包 CodeForces 433A - Kitahara Haruki&#39;s Gift

Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends. Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefor

POJ1837:Balance(01背包)

Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms of negligible weight and each arm's length is 15. Some hooks are attached to these arms and

POJ3624Charm Bracelet(01背包)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23359   Accepted: 10532 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro