繁体   English   中英

由水桶制成的塔-加快算法

[英]Tower made of buckets - speeding up alghoritm

我需要加快我的算法。 关于寻找塔的高度。 塔是用水桶建造的。 每个铲斗都有高度和半径(1 <=高度,半径<= 1000)。 变量bucketCount描述了在塔上放置了多少个bucket(1 <= bucketCount <= 10 6 )。 我们按顺序设置存储桶。 铲斗厚度为0(为简单起见)

示例塔的图像

示例塔的图像

我决定使用堆栈。 我对每个存储桶的算法:

  1. 如果堆栈为空,则将铲斗推入堆栈,
  2. 如果我握的水桶较窄,则将水桶放在顶部,然后将其放到堆栈上
  3. 其他情况下,我握住的水桶更宽:弹出并找到最大高度(对于新的水桶地面),然后推开我握住的水桶。

对于每个铲斗,我添加了附加的可变地面,用于指定将哪个高度的铲斗放置在哪个地面上。 同时,我将塔的最大高度保持可变。

我想这虽然花费太多时间,但我找不到办法。 有什么办法可以加快速度吗? 我使用了性能分析,并且我知道top()需要很多时间。

输入示例: 2 20 20 30 30
输出:50

#include <iostream>
#include <stack>

using namespace std;

class Bucket{
public:
    int height;
    int radius;
    int ground;
};

int main()
{
    stack<Bucket> tower;
    int hightestPoint = 0;
    int bucketCount;
    cin >> bucketCount;
    Bucket temp;
    int maksimum;
    int sum;
    for(int i = 0; i < bucketCount; i++){
        cin >> temp.radius >> temp.height;
        maksimum = -1;
        sum = 0;
        if(tower.empty()){
            temp.ground = 0;
            tower.push(temp);
        } else {
            if(temp.radius < tower.top().radius){ //If bucket is narrower then push it
                temp.ground = tower.top().ground;
                tower.push(temp);
            } else { //If bucket is wider
                while(!tower.empty() && temp.radius >= tower.top().radius){ //Pop and search for new ground (maximum)
                    sum = tower.top().height + tower.top().ground;
                    if(maksimum < sum){
                        maksimum = sum;
                    }
                    tower.pop();
                }
                temp.ground = maksimum; //Set ground for new bucket
                tower.push(temp);
            }
        }
        sum = tower.top().height + tower.top().ground; //Meantime find highest point in stack
        if(hightestPoint < sum){
            hightestPoint = sum;
        }
    }
    cout << hightestPoint << endl;

    return 0;
}

更新#1

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Bucket{
public:
    int height;
    int radius;
    int ground;
};

bool compareBuckets(Bucket a, Bucket b){
    if(a.radius > b.radius){
        return true;
    } else {
        return false;
    }
}

bool compareMax(Bucket a, Bucket b){
    if((a.height + a.ground) > (b.height + b.ground)){
        return true;
    } else {
        return false;
    }
}

int main()
{
    int ile;
   // cin >> ile;
   ile = 1;
    for(int p = 0; p< ile; p++){
        vector<Bucket> tower;
        int hightestPoint = 0;
        int bucketCount;
       // cin >> bucketCount;
        bucketCount = 1000000;
        Bucket temp;
        vector<Bucket>::iterator low;
        vector<Bucket>::iterator element;
        int sum;
        for(int i = 0; i < bucketCount; i++){
            //cin >> temp.radius >> temp.height;
            temp.radius = i % 1000;
            temp.height = i % 1000;
            sum = 0;
            if(tower.empty()){
                temp.ground = 0;
                tower.push_back(temp);
            } else {
                if(temp.radius < tower.back().radius){ //If bucket is narrower then push it
                    temp.ground = tower.back().ground;
                    tower.push_back(temp);
                } else { //If bucket is wider
                    low= lower_bound (tower.begin(), tower.end(), temp,compareBuckets);
                    element = max_element(low, tower.end(), compareMax);
                    Bucket b = tower.at(element-tower.begin());
                    temp.ground = b.ground + b.height;//Set ground for new bucket
                    tower.erase(low, tower.end());
                    tower.push_back(temp);
                }
            }
            sum = tower.back().height + tower.back().ground; //Meantime find highest point in stack
            if(hightestPoint < sum){
                hightestPoint = sum;
            }
        }
        cout << hightestPoint << endl;
    }

    return 0;
}

编辑:正如Matt所指出的,不需要任何编辑。

while语句在堆栈中循环,发现存储桶刚好比新存储桶宽。 这实际上是在列表中找到一个元素,它比某些新元素要小。 使用二进制搜索或将列表存储在集合中可以轻松完成。

为此,您可以使用vector来维护堆栈,然后使用lower_bound查找合适的存储桶。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM