Sei sulla pagina 1di 3

Immadi Pulikeshi

Problem: Immadi Pulikeshi is the Greatest king of all time. He is the king of the chalukyas of Badami.
Now he is ruling in the 2nd 3rd and 4th quadrants of the world. Now He wants to conquer the 1st
quadrant. In 1st quadrant there are several small dynasties. Some of the region are in dispute as it
belongs to more than one dynasty. A dispute is said to happen between dynasties when they share
common x-coordinate. He can only conquer one of the dynasties among those which share any
common x-coordinate. Each small dynasty is represented as a polygon. Immadi pulikeshi being the
greatest king wants to acquire as much land as possible. You are appointed as Mantri in his Cabinet. He
wants your help in selecting those dynasties which maximize his area of conquering.

Difficulty : Medium
Pre-Requisites : Co-Ordinate Geometry, Dynamic Programming
Approach : As the input is only the corners of the polygon first we have to find the area of the
polygon. We can find that using the shoelace formula. And each polygon extends from some Xmin coordinate to Xmax.
Now we have to find a subset of the Polygons in which no two polygons share a common x-coordinate
and sum of the area of the polygons in the subset is maximum.
We will sort the polygons according to their Xmax co-ordinate. So that whichever polygon finishes first
is at the beginning. For each polygon we have 2 options, We can either select it or not in the subset.
If we dont select it the area that we get is maximum of the area that we could get from the previous
polygons.

If we want to select the polygon then Xmin of this polygon should be greater than Xmax of the
previously picked polygon. And if the above condition is satisfied then we will select if picking the
current polygon maximizes the total area.
Links : Shoelace Formula
Weighted job Scheduling
Time complexity : O(N^2) where N is the number of polygons.
Code (C++) :
#include<bits/stdc++.h>
using namespace std;
class polygon {
int m;
vector<pair<int, int> > vertices;
double findArea(){
double a = 0.0;
int j = m-1;
for(int i=0; i<m; i++){
a += (vertices[j].first + vertices[i].first) * (vertices[j].second - vertices[i].second);
j = i;
}
area = abs(a/2.0);
}
public:
int xMin, xMax;
double area;
polygon(){
xMin = 101;
xMax = 0;
m = 0;
area = 0;
}
void scanVertices(){
int x, y;
scanf("%d", &m);
for(int i=0; i<m; i++){
scanf("%d%d", &x, &y);
vertices.push_back(make_pair(x, y));
xMin = min(xMin, x);
xMax = max(xMax, x);
}
findArea();
}
};

bool compare(polygon &a, polygon &b){


return (a.xMax < b.xMax);
}
int previous(vector<polygon> p, int index){
for(int j=index-1; j>=0; j--)
if(p[j].xMax < p[index].xMin)
return j;
return -1;
}
double selectMaxArea(vector<polygon> p){
double curArea, total[p.size()];
total[0] = p[0].area;
for(int i=1; i<p.size(); i++){
curArea = p[i].area;
int lnc = previous(p, i);
if(lnc != -1)
curArea += total[lnc];
total[i] = max(total[i-1], curArea);
}
return total[p.size()-1];
}
int main(){
int n;
vector<polygon> p;
scanf("%d", &n);
for(int i=0; i<n; i++){
polygon pi;
pi.scanVertices();
p.push_back(pi);
}
sort(p.begin(), p.end(), compare);
double ans = selectMaxArea(p);
printf("%.2lf\n", ans);
}

Potrebbero piacerti anche