刷題——Space Elevator POJ - 2392
/*
給定一些建築塊,問用這些建築塊能搭建多高的建築
每個建築塊有一定的高度h,和最高能搭建高度a,和塊數c
很明顯的這是一個揹包問題,揹包容量不超過40000
現在也就是求在限制範圍中所能搭建的最大高度
dp[i]表示i這個高度是否能達到
那麼dp[i]={
-1:不能達到
0:能達到,dp[i-e[i].h*d]=0,d[1,e[i].c]
}
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct node{int h,a,c;};
node e[405];
bool cmp(node a,node b){
return a.a<b.a;
}
int dp[40005];
int use[40005];
int main(){
int k;
while(~scanf("%d",&k)){
for(int i=0;i<k;i++){
scanf("%d %d %d",&e[i].h,&e[i].a,&e[i].c);
}
sort(e,e+k,cmp);
memset(dp,-1,sizeof(dp));
dp[0]=0;
int maxx=0;
for(int i=0;i<k;i++){
memset(use,0,sizeof(use));
for(int j=e[i].h;j<=e[i].a;j++){
if(dp[j]==-1&&dp[j-e[i].h]==0&&use[j-e[i].h]+1<=e[i].c){
dp[j]=0;
use[j]=use[j-e[i].h]+1;
if(j>maxx){
maxx=j;
}
}
}
}
printf("%d\n",maxx);
}
return 0;
}
給定一些建築塊,問用這些建築塊能搭建多高的建築
每個建築塊有一定的高度h,和最高能搭建高度a,和塊數c
很明顯的這是一個揹包問題,揹包容量不超過40000
現在也就是求在限制範圍中所能搭建的最大高度
dp[i]表示i這個高度是否能達到
那麼dp[i]={
-1:不能達到
0:能達到,dp[i-e[i].h*d]=0,d[1,e[i].c]
}
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct node{int h,a,c;};
node e[405];
bool cmp(node a,node b){
return a.a<b.a;
}
int dp[40005];
int use[40005];
int main(){
int k;
while(~scanf("%d",&k)){
for(int i=0;i<k;i++){
scanf("%d %d %d",&e[i].h,&e[i].a,&e[i].c);
}
sort(e,e+k,cmp);
memset(dp,-1,sizeof(dp));
dp[0]=0;
int maxx=0;
for(int i=0;i<k;i++){
memset(use,0,sizeof(use));
for(int j=e[i].h;j<=e[i].a;j++){
if(dp[j]==-1&&dp[j-e[i].h]==0&&use[j-e[i].h]+1<=e[i].c){
dp[j]=0;
use[j]=use[j-e[i].h]+1;
if(j>maxx){
maxx=j;
}
}
}
}
printf("%d\n",maxx);
}
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.