1. 程式人生 > >【poj 2392】Space Elevator(貪心+多重揹包)

【poj 2392】Space Elevator(貪心+多重揹包)

Space Elevator

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13340 Accepted: 6323

Description

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.

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.

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.

 我們先把石頭達到的最大高度排序後,這個問題就轉化成為了一個完全揹包問題。

但是我們這次需要遍歷一遍dp陣列來求得最大值。(因為最終的高度不確定,即揹包的容量就不確定)。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn = 1000500;
int n,m;
struct node
{
    int h,hh,num;
}s[maxn];
int dp[maxn];
bool cmp(node a,node b)
{
    return a.hh<b.hh;
}
void zopac(int v,int h)//01揹包
{
    for(int j=v;j>=h;j--)
    {
        dp[j]=max(dp[j],dp[j-h]+h);
    }
}
void copac(int v,int h)//完全揹包
{
    for(int j=h;j<=v;j++)
    {
        dp[j]=max(dp[j],dp[j-h]+h);
    }
}

void mupac(int n)//多重揹包
{
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++)
    {
        if(s[i].h*s[i].num>=s[i].hh) copac(s[i].hh,s[i].h);
        else{
            for(int k=1;k<=s[i].num;k<<=1)
            {
                zopac(s[i].hh,k*s[i].h);
                s[i].num-=k;
            }
            if(s[i].num) zopac(s[i].hh,s[i].num*s[i].h);
        }
    }
}
int main()
{
    cin>>n;
    int cnt=0;
    for(int i=0;i<n;i++){
         cin>>s[i].h>>s[i].hh>>s[i].num;
         cnt=max(cnt,s[i].hh);
    }
    sort(s,s+n,cmp);
    mupac(n);
    int ans=0;
    for(int i=0;i<=cnt;i++) ans=max(ans,dp[i]);
    cout<<ans<<endl;
    return 0;
}

相關推薦

poj 2392Space Elevator貪心+多重揹包

Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13340 Accepted: 6323 Description The cows ar

POJ - 2392Space Elevator dp,優秀的揹包問題

題幹: 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

BZOJ 2288 POJ Challenge生日禮物貪心+優先隊列

ace urn ons target challenge pri 最大 font return 【題目鏈接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2288 【題目大意】   給出一列數,求最多取m段

POJ - 1696Space Ant 凸包,最小極角,排序

題幹: The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet

POJ - 3253Fence Repair貪心,時光倒流

題幹: Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N

POJ - 1655Balancing Act 樹的重心

Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more

UVA - 1335Beijing Guards 貪心,二分

題幹: 題目大意: 有n個人為成一個圈,其中第i個人想要r[i]種不同的禮物,相鄰的兩個人可以聊天,炫耀自己的禮物。如果兩個相鄰的人擁有同一種禮物,則雙方都會很不高興,問最少需要多少種不同的禮物才能滿足所有人的需求,假設每種禮物有無限多個。 n<=100000 解題報告:

POJ 3004Subway planning極角排序+貪心

Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 1384Accepted: 375 Description The government in a foreign country is looking into the possibility

POJ 1716Integer Intervals差分約束系統

入門題 put AD edge ota 全部 lib 最小 最短 id=1716">【POJ 1716】Integer Intervals(差分約束系統) In

POJ - 2001 Shortest Prefixes 字典樹,查詢重複字首區間

題幹: A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "ca

POJ - 1556The Doors 計算幾何,線段相交

題幹: You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x

POJ - 2663Tri Tiling 簡單dp

題幹: In how many ways can you tile a 3xn rectangle with 2x1 dominoes?  Here is a sample tiling of a 3x12 rectangle.  Input Input c

POJ - 3273 Monthly Expense 二分,最大最小值

題幹: Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recor

POJ - 2226Muddy Fields匈牙利演算法 或 網路流dinic,二分圖匹配,最小點覆蓋,矩陣中優秀的建圖方式

題幹: Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the gra

POJ - 1664放蘋果 遞迴經典題 或 dp 或 母函式

題幹: 把M個同樣的蘋果放在N個同樣的盤子裡,允許有的盤子空著不放,問共有多少種不同的分法?(用K表示)5,1,1和1,5,1 是同一種分法。 Input 第一行是測試資料的數目t(0 <= t <= 20)。以下每行均包含二個整數M和N,以空格分開。1<=M,N&

CodeForces - 349ACinema Line 貪心(其實不是貪心),亂搞

題幹: The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them h

POJ 3368Frequent valuesRMQ

Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you a

POJ - 2135Farm Tour最小費用最大流

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first o

POJ - 2342Anniversary party樹形dp

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employe

POJ 3984迷宮問題DFS

Description 定義一個二維陣列:  int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一個迷宮