Poj 2392 Space Elevator

題目

Space Elevator

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

依然是背包問題斑胜,一共 k 種類型的磚塊控淡,分別給出每種磚塊的高度、最大可以放置的高度止潘、數(shù)量(例如第一組數(shù)據(jù)掺炭,分別為7 40 3)。
輸出:輸出可以壘起的最大高度

代碼

不做過多解釋凭戴,與 Poj 1742 類似涧狮,稍作修改,排序,然后依然 bitset A 掉勋篓。

#include <stdio.h>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;

struct Block {
    int height;
    int number;
    int maxAltitude;
};

bool compare(Block a,Block b) {
    return a.maxAltitude < b.maxAltitude;
}

int main(int argc, const char * argv[]) {
    
    int k;
    Block blockList[405];
    bitset<40005> resultBitSet;
    
    scanf("%d", &k);
    for (int i = 0; i < k; i++) {
        scanf("%d %d %d", &blockList[i].height, &blockList[i].maxAltitude, &blockList[i].number);
    }
    
    sort(blockList, blockList + k, compare);
    resultBitSet.reset();
    
    int shiftNumber = 0;
    for (int i = 0; i < k; i++) {
        for (int t = 1; t <= blockList[i].number && t * blockList[i].height <= blockList[i].maxAltitude; t++) {
            resultBitSet |= (resultBitSet << blockList[i].height);
            resultBitSet.set(t * blockList[i].height);
            
            //以下三行比較重要吧享,目的就是清除超過 maxAltitude 的數(shù)據(jù)
            shiftNumber = 40005 - (blockList[i].maxAltitude + 1);
            resultBitSet <<= shiftNumber;
            resultBitSet >>= shiftNumber;
        }
    }
    
    int result = 0;
    for (int i = blockList[k - 1].maxAltitude; i >= 1; i--) {
        if (resultBitSet.test(i)) {
            result = i;
            break;
        }
    }
    printf("%d\n", result);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市譬嚣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌钞它,老刑警劉巖拜银,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異遭垛,居然都是意外死亡尼桶,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門锯仪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來泵督,“玉大人庶喜,你說我怎么就攤上這事小腊。” “怎么了久窟?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵秩冈,是天一觀的道長。 經(jīng)常有香客問我斥扛,道長入问,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任稀颁,我火速辦了婚禮芬失,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘匾灶。我一直安慰自己棱烂,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布粘昨。 她就那樣靜靜地躺著垢啼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪张肾。 梳的紋絲不亂的頭發(fā)上芭析,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音吞瞪,去河邊找鬼馁启。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的惯疙。 我是一名探鬼主播翠勉,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼霉颠!你這毒婦竟也來了对碌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蒿偎,失蹤者是張志新(化名)和其女友劉穎朽们,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诉位,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡骑脱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了苍糠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片叁丧。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖岳瞭,靈堂內(nèi)的尸體忽然破棺而出拥娄,到底是詐尸還是另有隱情,我是刑警寧澤寝优,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布条舔,位于F島的核電站,受9級特大地震影響乏矾,放射性物質(zhì)發(fā)生泄漏孟抗。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一钻心、第九天 我趴在偏房一處隱蔽的房頂上張望凄硼。 院中可真熱鬧,春花似錦捷沸、人聲如沸摊沉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽说墨。三九已至,卻和暖如春苍柏,著一層夾襖步出監(jiān)牢的瞬間尼斧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工试吁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留棺棵,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像烛恤,于是被迫代替她去往敵國和親母怜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容