Using Disjoint Data Structures


Forum Post 0 Answers Using Disjoint Data Structures

Downtown Gotham is booming and the main road is now lined with a continuous row of skyscrapers. For aesthetic reasons, the Gotham Development Council had banned advertising hoardings along the main road.
The High Court has squashed the Council's ban as unconstitutional and ruled that rectangular hoardings may be put up parallel to the main road, along the walls of the skyscrapers lining the road. The only restriction is that a hoarding may not extend above the height of any building along its length. Advertising agencies across the city are buzzing with excitement at the news and accounts executives are busy calculating how large they can make their hoardings, in terms of total area, to collect maximum revenue from advertisers.
Suppose the skyline is represented as follows, where each skyscaper is represented by its height and width, both quantities measured in metres. The first line of input is an integer N, the number of skyscrapers. This is followed by N lines of input describing the skyscrapers in left to right order. Each of these lines consists of two integers W and H, specifying the width and height of the skyscraper, respectively. The task is to compute the maximum area that a hoarding can have for a given arrangement of skyscrapers.
8
20 30
10 50
30 70
20 50
10 10
20 55
30 80
20 40
It is my first program in C++, in Union-Find and using vectors(yeah too ambitious for the first problem). I wanted to know if the flow of the program is atleast partially correct. Besides, I am getting the error: “glibc detected” along with a wrong answer. So, should I start afresh or am I almost there?

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

#define N_MAX 100000

using namespace std;

vector<int> parent;
vector<int> rank;
//vector<int> length;
vector<int> ht;
vector<int> wt;

class skyscraper
{
public:
int height;
int width;
};


struct sortH
{
int i,height;
bool operator<(const sortH& p)const
{
return (height > p.height);
}
};

void make_set(int x,int height,int width)
{
parent[x] = x;
rank[x] = 0;
//length[x] = 1;
ht[x] = height;
wt[x] = width;
}
int find(int x)
{
if(parent[x] != x)
{
parent[x] = find(parent[x]);
}
return parent[x];
}
void set_union(int x,int y)
{
int root_x,root_y;
root_x = find(x);
root_y = find(y);
if(rank[root_x] < rank[root_y])
{
parent[root_x] = root_y;
}
else
{
parent[root_y] = root_x;
rank[root_x] += (rank[root_x] == rank[root_y]);
}
}

int main()
{
int N,i,j,k,l,x,y,z;
int H;
int W;
int Amax,LA,RA,A,S;
cin >> N;
//Union of children
parent.resize(N);
rank.resize(N);
ht.resize(N);
wt.resize(N);
////////////////////////

sortH p[N];
vector<skyscraper> skyline(N);

for(i = 0; i < N; i++)
{
cin >> skyline[i].width;
cin >> skyline[i].height;
p[i].height = skyline[i].height;
p[i].i = i;
make_set(i,skyline[i].height,skyline[i].width);
}
sort(p,p+N);
H = p[0].height;
W = skyline[p[0].i].width;
Amax = H * W;
for(i = 1; i < N; i++)
{

j = p[i].i;
k = j-1;
l = j+1;
S = skyline[j].height * skyline[j].width;
x = skyline[j].height;
y = skyline[k].height;
z = skyline[l].height;
if(x < y)
{
set_union(k,j);
ht[k] = x;
wt[k]+= skyline[k].width;
LA = ht[k] * wt[k];
}
if(x < z)
{
set_union(l,j);
ht[l] = x;
wt[l] += skyline[l].width;
RA = ht[l] * wt[l];
}
if(S < LA) S = LA;
if(S < RA) S = RA;

if(Amax < S) Amax = S;

}
// if(skyline[j].height
// }
// for(int i = 0; i < N; i++)
// {
// cout << skyline[i].height << ' ' << skyline[i].width << endl;
// }
// for(int i = 0; i < N; i++)
// {
cout << Amax << endl;
// }
}

- f2010190@pilani.bits-pilani.ac.in March 17, 2015 | Flag |


Comment hidden because of low score. Click to expand.
0
of 0 vote

This isn't really a union-find problem. It's similar to asking for the maximum area of a rectangle that can fit inside a histogram. I haven't looked at your solution, but if you're using union-find you probably misunderstood the question.

- Anonymous March 26, 2015 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More