【BZOJ】2021: [Usaco2010 Jan]Cheese Towers(dp)

http://www.lydsy.com/JudgeOnline/problem.php?id=2021

噗,自己太弱想不到。

原来是2次背包。

由于只要有一个大于k的高度的,而且这个必须放在最顶,那么我们就可以枚举每一个比k大的放在最顶,其它的都放在下边即可。

还有,注意这是完全背包!

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <iostream>

#include <algorithm>

#include <queue>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)

#define for1(i,a,n) for(int i=(a);i<=(n);++i)

#define for2(i,a,n) for(int i=(a);i<(n);++i)

#define for3(i,a,n) for(int i=(a);i>=(n);--i)

#define for4(i,a,n) for(int i=(a);i>(n);--i)

#define CC(i,a) memset(i,a,sizeof(i))

#define read(a) a=getint()

#define print(a) printf("%d", a)

#define dbg(x) cout << #x << " = " << x << endl

#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }

#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl

inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

inline const int max(const int &a, const int &b) { return a>b?a:b; }

inline const int min(const int &a, const int &b) { return a<b?a:b; }



const int N=105, M=1005;

int f[N][M], n, T, K, w[N], h[N], hh[N], ans;



int main() {

	read(n); read(T); read(K);

	for1(i, 1, n) read(w[i]), read(h[i]), hh[i]=h[i]/5*4;

	for1(i, 1, n) if(h[i]<K)

		for1(j, h[i], T)

			f[0][j]=max(f[0][j], f[0][j-h[i]]+w[i]);

	for1(i, 1, n) if(h[i]>=K) {

		f[i][h[i]]=w[i];

		for1(j, 1, n)

			for1(v, h[i]+hh[j], T)

				f[i][v]=max(f[i][v], f[i][v-hh[j]]+w[j]);

	}

	for1(i, 0, n) for1(j, h[i], T) ans=max(ans, f[i][j]);

	print(ans);

	return 0;

}

 

 


 

 

Description

Farmer John wants to save some blocks of his cows' delicious Wisconsin cheese varieties in his cellar for the coming winter. He has room for one tower of cheese in his cellar, and that tower's height can be at most T (1 <= T <= 1,000). The cows have provided him with a virtually unlimited number of blocks of each kind of N (1 <= N <= 100) different types of cheese (conveniently numbered 1..N). He'd like to store (subject to the constraints of height) the most valuable set of blocks he possibly can. The cows will sell the rest to support the orphan calves association. Each block of the i-th type of cheese has some value V_i (1 <= V_i <= 1,000,000) and some height H_i (5 <= H_i <= T), which is always a multiple of 5. Cheese compresses. A block of cheese that has height greater than or equal to K (1 <= K <= T) is considered "large" and will crush any and all of the cheese blocks (even other large ones) located below it in the tower. A crushed block of cheese doesn't lose any value, but its height reduces to just 4/5 of its old height. Because the height of a block of cheese is always a multiple of 5, the height of a crushed block of cheese will always be an integer. A block of cheese is either crushed or not crushed; having multiple large blocks above it does not crush it more. Only tall blocks of cheese crush other blocks; aggregate height of a tower does not affect whether a block is crushed or not. What is the total value of the best cheese tower FJ can construct? Consider, for example, a cheese tower whose maximum height can be 53 to be build from three types of cheese blocks. Large blocks are those that are greater than or equal to 25. Below is a chart of the values and heights of the various cheese blocks he stacks: Type Value Height 1 100 25 2 20 5 3 40 10 FJ constructs the following tower: Type Height Value top -> [1] 25 100 [2] 4 20 <- crushed by [1] above [3] 8 40 <- crushed by [1] above [3] 8 40 <- crushed by [1] above bottom -> [3] 8 40 <- crushed by [1] above The topmost cheese block is so large that the blocks below it are crushed. The total height is: 25 + 4 + 8 + 8 + 8 = 53 The total height does not exceed 53 and thus is 'legal'. The total value is: 100 + 20 + 40 + 40 + 40 = 240. This is the best tower for this particular set of cheese blocks. John要建一个奶酪塔,高度最大为T。他有N块奶酪。第i块高度为Hi(一定是5的倍数),价值为Vi。一块高度>=K的奶酪被称为大奶酪,一个奶酪如果在它上方有大奶酪(多块只算一次),它的高度就会变成原来的4/5.。。 很显然John想让他的奶酪他价值和最大。。 求这个最大值。。

Input

第一行分别是 N T K 接下来N行分别是 Vi Hi

Output

一行最大值

Sample Input

3 53 25
100 25
20 5
40 10



Sample Output

240

HINT

Source

你可能感兴趣的:(USACO)