2392 -- Space Elevator
这题要思考的方向是往多重背包这边想,高度既是“价值”又是“重量”,背包的容积是变动的a_i。性价比恒为1,优先使用a_i小的。必须按积木的a_i从小到大的顺序递推才能覆盖全部解空间,否则a_i小的积木根本码不上去。
http://www.cnblogs.com/kedebug/archive/2013/02/06/2908248.html
标准解法
http://programming-study-notes.blogspot.com/2014/04/poj-2392-space-elevator.html
且最後要枚舉一下dp的每個値找出最大値,因為最大値不一定位在dp的最後一個位置。
http://www.java3z.com/cwbwebhome/article/article17/acm847.html
Read full article from 2392 -- Space Elevator
The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000).
Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.
Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.
Input
* Line 1: A single integer, K
* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.
* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.
Output
* Line 1: A single integer H, the maximum height of a tower that can be built
Sample Input
3
7 40 3
5 23 8
2 52 6
Sample Output
48
Hint
OUTPUT DETAILS:
From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.
http://www.bubuko.com/infodetail-437007.htmlFrom the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.
本题是一道多重背包问题, 不过每个物品的选择不仅仅要受该种物品的数量num[i]限制, 且该物品还受到limit[i]的限制.
这里有一个贪心的结论:
我们每次背包选取物品时都应该优先放置当前limit[i]值最小的积木(可以画个图看看,不过不太好证明该结论). 所以我们首先把所有积木按limit[i]的值进行从小到大的排序, 然后从1编号开始选积木即可.
在进行多重背包之前要进行一次排序,将最大高度小的放在前面,只有这样才能得到最优解,如果
将大的放在前面,后面有的小的就不能取到,排序之后就可以进行多重背包了
下面就是多重背包的过程了.
令dp[i][j]==x表示用前i个积木且总的高度<=j时能达到的最大高度为x.
初始化: dp全为0.
对于每种物品, 我们要做两种选择:
1. num[i]*high[i]>=limit[i]时, 做一次完全背包.
2. Num[i]*high[i]<limit[i]时, 需要把当前物品再分类, 然后做多次01背包即可.
最终所求: dp[n][j]的最大值. 其中j遍历[0,limit[n]]内所有数.
http://www.hankcs.com/program/cpp/poj-2392-space-elevator.html这题要思考的方向是往多重背包这边想,高度既是“价值”又是“重量”,背包的容积是变动的a_i。性价比恒为1,优先使用a_i小的。必须按积木的a_i从小到大的顺序递推才能覆盖全部解空间,否则a_i小的积木根本码不上去。
int
f[2][40000 + 16];
pair <
int
,
int
> q[40000 + 16];
// 单调队列 first := 最优解 second:= 容积
struct
Block
{
int
h, a, c;
bool
operator < (
const
Block & b)
const
{
return
a < b.a;
}
};
Block block[400 + 16];
for
(
int
i = 0; i < K; ++i)
{
cin >> block[i].h >> block[i].a >> block[i].c;
}
sort(block, block + K);
int
cur = 0, pre = 1;
for
(
int
i = 0; i < K; ++i)
{
swap(cur, pre);
int
v, w, m;
v = block[i].h;
w = block[i].h;
m = block[i].c;
int
V = block[i].a;
// 价值 重量 数量
for
(
int
mod = 0; mod < w; ++mod)
{
int
l = 0, r = 0;
// 单调队列 首 尾
for
(
int
j = 0; mod + j * w <= V; ++j)
{
// 维持单调队列的递减特性
while
(r > l && (f[pre][mod + j * w] - j * v) > q[r - 1].first)
{
// 将前面小的元素都挤出去
r--;
}
// 入队
q[r++] = make_pair(f[pre][mod + j * w] - j * v, j);
if
(r - l > 0 && j - q[l].second > m)
{
// 队列不为空,最优解对应的缺口无法填满,出队
l++;
}
f[cur][mod + j * w] = q[l].first + j * v;
}
}
}
cout << *max_element(f[cur], f[cur] + block[K - 1].a + 1) << endl;
http://www.cnblogs.com/kedebug/archive/2013/02/06/2908248.html
标准解法
void ZeroOnePack(int w, int val, int vol)
{
for (int v = vol; v >= w; --v)
dp[v] = max(dp[v], dp[v - w] + val);
}
void CompletePack(int w, int val, int vol)
{
for (int v = w; v <= vol; ++v)
dp[v] = max(dp[v], dp[v - w] + val);
}
void MultiplyPack(int w, int val, int num, int vol)
{
if (w * num >= vol)
{
CompletePack(w, val, vol);
return ;
}
int k = 1;
while (k <= num)
{
ZeroOnePack(w * k, val * k, vol);
num -= k;
k <<= 1;
}
if (num)
ZeroOnePack(w * num, val * num, vol);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d %d %d", &type[i].h, &type[i].a, &type[i].c);
sort(type, type + n);
for (int i = 0; i < n; ++i)
{
MultiplyPack(type[i].h, type[i].h, type[i].c, type[i].a);
for (int v = type[i].a; v <= type[n-1].a; ++v)
dp[v] = dp[type[i].a];
}
printf("%d\n", dp[type[n-1].a]);
return 0;
}
http://programming-study-notes.blogspot.com/2014/04/poj-2392-space-elevator.html
且最後要枚舉一下dp的每個値找出最大値,因為最大値不一定位在dp的最後一個位置。
struct Block{ int h; | |
int a; | |
int c; | |
}b[401]; | |
bool cmp(const Block &a, const Block &b) | |
{ | |
return a.a < b.a; | |
} | |
int main() | |
{ | |
int K; | |
while (scanf("%d", &K) != EOF) { | |
// Input | |
for (int k = 0; k < K; ++k) | |
scanf("%d %d %d", &b[k].h, &b[k].a, &b[k].c); | |
sort(b, b+K, cmp); | |
// Knapsack | |
fill(dp, dp+40005, 0); | |
for (int k = 0; k < K; ++k) | |
for (int i = 0; i < b[k].c; ++i) | |
for (int j = b[k].a; j-b[k].h >= 0; --j) | |
dp[j] = max(dp[j], dp[j-b[k].h] + b[k].h); | |
// Ouput | |
int ans = 0; | |
for (int i = 0; i <= b[K-1].a; ++i) | |
ans = max(ans, dp[i]); | |
printf("%d\n", ans); | |
} | |
} |
public class Main{ private int k; private Block block[]; private boolean access[]=new boolean[400001]; public Main(int k,Block block[]){ this.k=k; this.block=block; } private int doIt(){ int maxs=0; access[0]=true; Arrays.sort(block); for(int i=0;i< k;i++) { int t=0; int tmp=maxs; for(int j=maxs;j>=t;j--) { if(access[j]) for(int h=1;h<=block[i].c;h++) { int x=h*block[i].h+j; if(x>block[i].a) break; if(tmp< x) tmp=x; access[x]=true; } } maxs=tmp; } return maxs; } public static void main(String args[]){ Scanner sc=new Scanner(System.in); int k=sc.nextInt(); Block b[]=new Block[k]; for(int i=0;i< k;i++){ b[i]=new Block(); b[i].h=sc.nextInt(); b[i].a=sc.nextInt(); b[i].c=sc.nextInt(); } Main m=new Main(k,b); System.out.println(m.doIt()); } } class Block implements Comparable{//石头类型 int h;//石头的高度 int a;//最大建造高度 int c;//这种石头的数量 public int compareTo(Object o){ Block bl = (Block)o; return (int)(this.a - bl.a); }