码迷,mamicode.com
首页 > 其他好文 > 详细

P2979 [USACO10JAN]奶酪塔Cheese Towers

时间:2019-08-02 22:37:29      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:values   top   rtu   integer   height   otto   which   one   diff   

题意翻译

题目描述

FJ要建一个奶酪塔,高度最大为T。他有N种奶酪。第i种奶酪的高度为Hi(一定是5的倍数),价值为Vi。一块高度Hi>=K的奶酪被称为大奶酪,一个奶酪如果在它上方有大奶酪(如果有多块就只算一次),它的高度Hi就会变成原来的4/5.。FJ想让他的奶酪塔价值和最大。请你求出这个最大值。

输入格式:

第一行三个数N,T,K,意义如上所述。 接下来n行,每行两个数V_i,h_i(注意顺序)

输出格式:

奶酪塔的最大价值

题目描述

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. 要建一个奶酪塔,高度最大为T。他有N块奶酪。第i块高度为Hi(一定是5的倍数),价值为Vi。一块高度>=K的奶酪被称为大奶酪,一个奶酪如果在它上方有大奶酪(多块只算一次),它的高度就会变成原来的4/5.。 很显然John想让他的奶酪他价值和最大。求这个最大值。

输入格式:

  • Line 1: Three space-separated integers: N, T, and K

  • Lines 2..N+1: Line i+1 contains two space separated integers: V_i and H_i

输出格式:

  • Line 1: The value of the best tower FJ can build

输入样例:

3 53 25

100 25

20 5

40 10

输出样例:

240

思路:

如果没有大奶酪,这道题就是一道裸的完全背包。

稍微思考能够发现,最有解只有两种情况:要么整个奶酪塔上都没有大奶酪,要么奶酪塔的最上面一块是大奶酪。因为如果在奶酪塔的中间位置有一个大奶酪,那么显然把这块大奶酪提到奶酪塔的最顶端不会使答案变劣。

对于第一种情况,直接做完全背包即可。

对于第二种情况,我们可以枚举最上面的那块大奶酪i,用v[i]+f[(T-h[i])*5/4]更新ans。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=1010;

int n,t,k,ans;
int f[2*N],v[N],h[N];

int main () {
    scanf("%d%d%d",&n,&t,&k);
    for (int i=1; i<=n; i++) {
        scanf("%d%d",&v[i],&h[i]);
        for(int j=h[i]; j<=t*5/4; j++)
            f[j]=max(f[j],f[j-h[i]]+v[i]);
    }
    ans=f[t];
    for(int i=1; i<=n; i++)
        if (h[i]>=k)
            ans=max(ans,f[(t-h[i])*5/4]+v[i]);
    printf("%d\n",ans);
    return 0;
}

 

最后两种情况取max即可。

代码:

P2979 [USACO10JAN]奶酪塔Cheese Towers

标签:values   top   rtu   integer   height   otto   which   one   diff   

原文地址:https://www.cnblogs.com/mysh/p/11291363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!