Compute the height of a tower made of buckets
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
8
down vote
favorite
I need to speed up my algorithm. It is about finding the height of a tower. The tower is built from buckets. Each bucket has height and radius (1 <= height,radius <= 1000). Variable bucketCount
describe how many buckets are placed on tower (1 <= bucketCount
<= 10^6). We set buckets in sequence. Thickness of bucket is 0 (for simplicity)
Image of example tower:
I decided to use a stack. My algorithm for each bucket:
- If stack is empty, then push bucket to stack.
- If the bucket I hold is narrower then bucket on top then put it on the stack.
- Else while bucket I hold is wider: pop and find maximum height (for new bucket ground), after that push bucket which I hold.
For each bucket I added additional an variable ground
, which specifies the height at which the bucket is placed. Meantime I keep maximum height of tower in variable.
This task is automatically checked and gives output: time limit exceeded. I tried to optimize code but nothing gives positive result.
Example input: 2 20 20 30 30
Output: 50
#include <iostream>
#include <stack>
using namespace std;
struct Bucket
public:
int height;
int radius;
int ground;
;
int main()
int ile;
cin >> ile;
for(int c = 0; c < ile; c++)
stack<Bucket> tower;
int hightestPoint = 0;
int bucketCount;
cin >> bucketCount;
Bucket temp;
Bucket temporary; //Used to speed up a bit
int maksimum;
int sum;
int counter = 0; //Its size of the Stack. Used it because thought its faster then empty() function
for(int i = 0; i < bucketCount; i++)
cin >> temp.radius >> temp.height;
maksimum = -1;
sum = 0;
if(counter == 0)
temp.ground = 0;
tower.push(temp);
counter++;
else
if(temp.radius < tower.top().radius) //If bucket is narrower then push it
temp.ground = tower.top().ground;
tower.push(temp);
counter++;
else //If bucket is wider
while((counter > 0) && temp.radius >= (temporary = tower.top()).radius) //Pop and search for new ground (maximum)
sum = temporary.height + temporary.ground;
if(maksimum < sum)
maksimum = sum;
tower.pop();
counter--;
temp.ground = maksimum; //Set ground for new bucket
tower.push(temp);
counter++;
sum = temp.height + temp.ground; //Meantime find highest point in stack
if(hightestPoint < sum)
hightestPoint = sum;
cout << hightestPoint << endl;
return 0;
c++ algorithm time-limit-exceeded stack complexity
add a comment |Â
up vote
8
down vote
favorite
I need to speed up my algorithm. It is about finding the height of a tower. The tower is built from buckets. Each bucket has height and radius (1 <= height,radius <= 1000). Variable bucketCount
describe how many buckets are placed on tower (1 <= bucketCount
<= 10^6). We set buckets in sequence. Thickness of bucket is 0 (for simplicity)
Image of example tower:
I decided to use a stack. My algorithm for each bucket:
- If stack is empty, then push bucket to stack.
- If the bucket I hold is narrower then bucket on top then put it on the stack.
- Else while bucket I hold is wider: pop and find maximum height (for new bucket ground), after that push bucket which I hold.
For each bucket I added additional an variable ground
, which specifies the height at which the bucket is placed. Meantime I keep maximum height of tower in variable.
This task is automatically checked and gives output: time limit exceeded. I tried to optimize code but nothing gives positive result.
Example input: 2 20 20 30 30
Output: 50
#include <iostream>
#include <stack>
using namespace std;
struct Bucket
public:
int height;
int radius;
int ground;
;
int main()
int ile;
cin >> ile;
for(int c = 0; c < ile; c++)
stack<Bucket> tower;
int hightestPoint = 0;
int bucketCount;
cin >> bucketCount;
Bucket temp;
Bucket temporary; //Used to speed up a bit
int maksimum;
int sum;
int counter = 0; //Its size of the Stack. Used it because thought its faster then empty() function
for(int i = 0; i < bucketCount; i++)
cin >> temp.radius >> temp.height;
maksimum = -1;
sum = 0;
if(counter == 0)
temp.ground = 0;
tower.push(temp);
counter++;
else
if(temp.radius < tower.top().radius) //If bucket is narrower then push it
temp.ground = tower.top().ground;
tower.push(temp);
counter++;
else //If bucket is wider
while((counter > 0) && temp.radius >= (temporary = tower.top()).radius) //Pop and search for new ground (maximum)
sum = temporary.height + temporary.ground;
if(maksimum < sum)
maksimum = sum;
tower.pop();
counter--;
temp.ground = maksimum; //Set ground for new bucket
tower.push(temp);
counter++;
sum = temp.height + temp.ground; //Meantime find highest point in stack
if(hightestPoint < sum)
hightestPoint = sum;
cout << hightestPoint << endl;
return 0;
c++ algorithm time-limit-exceeded stack complexity
I used radius to determine if bucket will be placed inside another bucket or on top of another bucket. There is no relationship. Radius and height can be any number less then 1000. U can replace word "radius" with "width"
â Th3NiKo
Apr 29 at 13:39
Is radius an integer?
â vnp
Apr 29 at 18:01
@vnp Yes, radius and height are integers.
â Th3NiKo
Apr 29 at 18:21
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I need to speed up my algorithm. It is about finding the height of a tower. The tower is built from buckets. Each bucket has height and radius (1 <= height,radius <= 1000). Variable bucketCount
describe how many buckets are placed on tower (1 <= bucketCount
<= 10^6). We set buckets in sequence. Thickness of bucket is 0 (for simplicity)
Image of example tower:
I decided to use a stack. My algorithm for each bucket:
- If stack is empty, then push bucket to stack.
- If the bucket I hold is narrower then bucket on top then put it on the stack.
- Else while bucket I hold is wider: pop and find maximum height (for new bucket ground), after that push bucket which I hold.
For each bucket I added additional an variable ground
, which specifies the height at which the bucket is placed. Meantime I keep maximum height of tower in variable.
This task is automatically checked and gives output: time limit exceeded. I tried to optimize code but nothing gives positive result.
Example input: 2 20 20 30 30
Output: 50
#include <iostream>
#include <stack>
using namespace std;
struct Bucket
public:
int height;
int radius;
int ground;
;
int main()
int ile;
cin >> ile;
for(int c = 0; c < ile; c++)
stack<Bucket> tower;
int hightestPoint = 0;
int bucketCount;
cin >> bucketCount;
Bucket temp;
Bucket temporary; //Used to speed up a bit
int maksimum;
int sum;
int counter = 0; //Its size of the Stack. Used it because thought its faster then empty() function
for(int i = 0; i < bucketCount; i++)
cin >> temp.radius >> temp.height;
maksimum = -1;
sum = 0;
if(counter == 0)
temp.ground = 0;
tower.push(temp);
counter++;
else
if(temp.radius < tower.top().radius) //If bucket is narrower then push it
temp.ground = tower.top().ground;
tower.push(temp);
counter++;
else //If bucket is wider
while((counter > 0) && temp.radius >= (temporary = tower.top()).radius) //Pop and search for new ground (maximum)
sum = temporary.height + temporary.ground;
if(maksimum < sum)
maksimum = sum;
tower.pop();
counter--;
temp.ground = maksimum; //Set ground for new bucket
tower.push(temp);
counter++;
sum = temp.height + temp.ground; //Meantime find highest point in stack
if(hightestPoint < sum)
hightestPoint = sum;
cout << hightestPoint << endl;
return 0;
c++ algorithm time-limit-exceeded stack complexity
I need to speed up my algorithm. It is about finding the height of a tower. The tower is built from buckets. Each bucket has height and radius (1 <= height,radius <= 1000). Variable bucketCount
describe how many buckets are placed on tower (1 <= bucketCount
<= 10^6). We set buckets in sequence. Thickness of bucket is 0 (for simplicity)
Image of example tower:
I decided to use a stack. My algorithm for each bucket:
- If stack is empty, then push bucket to stack.
- If the bucket I hold is narrower then bucket on top then put it on the stack.
- Else while bucket I hold is wider: pop and find maximum height (for new bucket ground), after that push bucket which I hold.
For each bucket I added additional an variable ground
, which specifies the height at which the bucket is placed. Meantime I keep maximum height of tower in variable.
This task is automatically checked and gives output: time limit exceeded. I tried to optimize code but nothing gives positive result.
Example input: 2 20 20 30 30
Output: 50
#include <iostream>
#include <stack>
using namespace std;
struct Bucket
public:
int height;
int radius;
int ground;
;
int main()
int ile;
cin >> ile;
for(int c = 0; c < ile; c++)
stack<Bucket> tower;
int hightestPoint = 0;
int bucketCount;
cin >> bucketCount;
Bucket temp;
Bucket temporary; //Used to speed up a bit
int maksimum;
int sum;
int counter = 0; //Its size of the Stack. Used it because thought its faster then empty() function
for(int i = 0; i < bucketCount; i++)
cin >> temp.radius >> temp.height;
maksimum = -1;
sum = 0;
if(counter == 0)
temp.ground = 0;
tower.push(temp);
counter++;
else
if(temp.radius < tower.top().radius) //If bucket is narrower then push it
temp.ground = tower.top().ground;
tower.push(temp);
counter++;
else //If bucket is wider
while((counter > 0) && temp.radius >= (temporary = tower.top()).radius) //Pop and search for new ground (maximum)
sum = temporary.height + temporary.ground;
if(maksimum < sum)
maksimum = sum;
tower.pop();
counter--;
temp.ground = maksimum; //Set ground for new bucket
tower.push(temp);
counter++;
sum = temp.height + temp.ground; //Meantime find highest point in stack
if(hightestPoint < sum)
hightestPoint = sum;
cout << hightestPoint << endl;
return 0;
c++ algorithm time-limit-exceeded stack complexity
edited Jul 23 at 20:47
Toby Speight
17.4k13488
17.4k13488
asked Apr 29 at 13:16
Th3NiKo
414
414
I used radius to determine if bucket will be placed inside another bucket or on top of another bucket. There is no relationship. Radius and height can be any number less then 1000. U can replace word "radius" with "width"
â Th3NiKo
Apr 29 at 13:39
Is radius an integer?
â vnp
Apr 29 at 18:01
@vnp Yes, radius and height are integers.
â Th3NiKo
Apr 29 at 18:21
add a comment |Â
I used radius to determine if bucket will be placed inside another bucket or on top of another bucket. There is no relationship. Radius and height can be any number less then 1000. U can replace word "radius" with "width"
â Th3NiKo
Apr 29 at 13:39
Is radius an integer?
â vnp
Apr 29 at 18:01
@vnp Yes, radius and height are integers.
â Th3NiKo
Apr 29 at 18:21
I used radius to determine if bucket will be placed inside another bucket or on top of another bucket. There is no relationship. Radius and height can be any number less then 1000. U can replace word "radius" with "width"
â Th3NiKo
Apr 29 at 13:39
I used radius to determine if bucket will be placed inside another bucket or on top of another bucket. There is no relationship. Radius and height can be any number less then 1000. U can replace word "radius" with "width"
â Th3NiKo
Apr 29 at 13:39
Is radius an integer?
â vnp
Apr 29 at 18:01
Is radius an integer?
â vnp
Apr 29 at 18:01
@vnp Yes, radius and height are integers.
â Th3NiKo
Apr 29 at 18:21
@vnp Yes, radius and height are integers.
â Th3NiKo
Apr 29 at 18:21
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
You don't need to remember every single bucket you've seen.
Once a bucket is added to the top of the pile, any wider bucket that has its rim lower than the rim of the new bucket becomes irrelevant, as do all the narrower buckets it sits on.
So a better data structure would just store the altitudes of all rims currently in play; this can be initialised to (0,0). When you add a bucket, find the widest rim not greater than the new bucket's width; we now set height[0] to that height and height[bucket_width] to height[0]+bucket_height. Remove all narrower buckets, and all other buckets up to a the new height - they can no longer affect the result.
Other items for review:
using namespace std;
Bringing all names in from a namespace is problematic;
namespace std
particularly so. See Why is âÂÂusing namespace stdâ considered bad practice?.Spellings:
highestPoint
,maximum
.- Naming:
temp
andtemporary
- these are declared far from their use, so it's hard to appreciate the difference. - You can omit
return 0;
inmain()
.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You don't need to remember every single bucket you've seen.
Once a bucket is added to the top of the pile, any wider bucket that has its rim lower than the rim of the new bucket becomes irrelevant, as do all the narrower buckets it sits on.
So a better data structure would just store the altitudes of all rims currently in play; this can be initialised to (0,0). When you add a bucket, find the widest rim not greater than the new bucket's width; we now set height[0] to that height and height[bucket_width] to height[0]+bucket_height. Remove all narrower buckets, and all other buckets up to a the new height - they can no longer affect the result.
Other items for review:
using namespace std;
Bringing all names in from a namespace is problematic;
namespace std
particularly so. See Why is âÂÂusing namespace stdâ considered bad practice?.Spellings:
highestPoint
,maximum
.- Naming:
temp
andtemporary
- these are declared far from their use, so it's hard to appreciate the difference. - You can omit
return 0;
inmain()
.
add a comment |Â
up vote
2
down vote
You don't need to remember every single bucket you've seen.
Once a bucket is added to the top of the pile, any wider bucket that has its rim lower than the rim of the new bucket becomes irrelevant, as do all the narrower buckets it sits on.
So a better data structure would just store the altitudes of all rims currently in play; this can be initialised to (0,0). When you add a bucket, find the widest rim not greater than the new bucket's width; we now set height[0] to that height and height[bucket_width] to height[0]+bucket_height. Remove all narrower buckets, and all other buckets up to a the new height - they can no longer affect the result.
Other items for review:
using namespace std;
Bringing all names in from a namespace is problematic;
namespace std
particularly so. See Why is âÂÂusing namespace stdâ considered bad practice?.Spellings:
highestPoint
,maximum
.- Naming:
temp
andtemporary
- these are declared far from their use, so it's hard to appreciate the difference. - You can omit
return 0;
inmain()
.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You don't need to remember every single bucket you've seen.
Once a bucket is added to the top of the pile, any wider bucket that has its rim lower than the rim of the new bucket becomes irrelevant, as do all the narrower buckets it sits on.
So a better data structure would just store the altitudes of all rims currently in play; this can be initialised to (0,0). When you add a bucket, find the widest rim not greater than the new bucket's width; we now set height[0] to that height and height[bucket_width] to height[0]+bucket_height. Remove all narrower buckets, and all other buckets up to a the new height - they can no longer affect the result.
Other items for review:
using namespace std;
Bringing all names in from a namespace is problematic;
namespace std
particularly so. See Why is âÂÂusing namespace stdâ considered bad practice?.Spellings:
highestPoint
,maximum
.- Naming:
temp
andtemporary
- these are declared far from their use, so it's hard to appreciate the difference. - You can omit
return 0;
inmain()
.
You don't need to remember every single bucket you've seen.
Once a bucket is added to the top of the pile, any wider bucket that has its rim lower than the rim of the new bucket becomes irrelevant, as do all the narrower buckets it sits on.
So a better data structure would just store the altitudes of all rims currently in play; this can be initialised to (0,0). When you add a bucket, find the widest rim not greater than the new bucket's width; we now set height[0] to that height and height[bucket_width] to height[0]+bucket_height. Remove all narrower buckets, and all other buckets up to a the new height - they can no longer affect the result.
Other items for review:
using namespace std;
Bringing all names in from a namespace is problematic;
namespace std
particularly so. See Why is âÂÂusing namespace stdâ considered bad practice?.Spellings:
highestPoint
,maximum
.- Naming:
temp
andtemporary
- these are declared far from their use, so it's hard to appreciate the difference. - You can omit
return 0;
inmain()
.
answered Jul 23 at 15:29
Toby Speight
17.4k13488
17.4k13488
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193205%2fcompute-the-height-of-a-tower-made-of-buckets%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I used radius to determine if bucket will be placed inside another bucket or on top of another bucket. There is no relationship. Radius and height can be any number less then 1000. U can replace word "radius" with "width"
â Th3NiKo
Apr 29 at 13:39
Is radius an integer?
â vnp
Apr 29 at 18:01
@vnp Yes, radius and height are integers.
â Th3NiKo
Apr 29 at 18:21