Saturday 17 October 2015

B. Layer Cake
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n rectangular cake layers. The length and the width of the i-th cake layer were ai and bi respectively, while the height of each cake layer was equal to one.
From a cooking book Dasha learned that a cake must have a form of a rectangular parallelepiped constructed from cake layers of the same sizes.
Dasha decided to bake the biggest possible cake from the bought cake layers (possibly, using only some of them). It means that she wants the volume of the cake to be as big as possible. To reach this goal, Dasha can cut rectangular pieces out of the bought cake layers. She always cuts cake layers in such a way that cutting lines are parallel to the edges of that cake layer. Dasha isn't very good at geometry, so after cutting out a piece from the original cake layer, she throws away the remaining part of it. Also she can rotate a cake layer in the horizontal plane (swap its width and length).
Dasha wants her cake to be constructed as a stack of cake layers of the same sizes. Each layer of the resulting cake should be made out of only one cake layer (the original one or cut out from the original cake layer).
Help Dasha to calculate the maximum possible volume of the cake she can bake using given cake layers.
Input
The first line contains an integer n (1 ≤ n ≤ 4000) — the number of cake layers that Dasha can use.
Each of the following n lines contains two integer numbers ai and bi (1 ≤ ai, bi ≤ 106) — the length and the width of i-th cake layer respectively.
Output
The first line of the output should contain the maximum volume of cake that can be baked using given layers.
The second line of the output should contain the length and the width of the resulting cake. If there are many solutions with maximum possible volume, print any of them.
Sample test(s)
input
5
5 12
1 1
4 6
6 4
4 6
output
96
6 4
input
2
100001 900000
900001 100000
output
180000000000
900000 100000
Note
In the first example Dasha doesn't use the second cake layer. She cuts 4 × 6 rectangle from the first cake layer and she uses other cake layers as is.
In the second example Dasha cuts off slightly from the both cake layers.
This was a very good implementation. Firstly we take the input in such a manner that
l[i]<b[i]  for all 1<i<=n. Then we sort the array.

Suppose our array in the sorted manner looks 

a b
c d
e f
g h

few inferences that can be drawn are

 a<c so every piece c,e,g can be carved in such a manner that we get one side as a.

Now coming to the breadths, there is no specific order of the breadths.
Suppose the breadths are in order like 

 d>f>b>h

So we push them in a PQ.  Now we select the max element i.e d we are for sure that we can 
obtain a piece of 1*a*d (how?  carve out a from c and let the d remain). We can have two pieces 
of length  a and breadth f (how? e->a, f   and c->a d->f ) similarly we can have 3*a*b and 4*a*h.

We can use all this possible combinations to generate the best answer for all  lengths a ,c,e,g.

Here goes the code

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
class Prioritize
{
public:
    int operator() ( const int & p1, const int & p2 )
    {
        return p1> p2;
    }
};
int main()
 {
  int n; priority_queue<ll > Q;
   cin>>n;
   vector<pair<ll,ll> > v;
   for(int i=0;i<n;i++)
   {

      ll a,b;
        cin>>a>>b;
        if(a>b)
            v.push_back(make_pair(a,b));
            else
            v.push_back(make_pair(b,a));
     }
      sort(v.begin(),v.end());
      int X,Y;
      ll ans=0;
      for(int i=0;i<n;i++)
      {
               
              ll x=v[i].ff;
              ll cnt=0;
              for(int j=i;j<n;j++)
              {
                        Q.push(v[j].ss);
}
while(!Q.empty())
{
    cnt++;
    ll y=Q.top();
    ll temp=cnt*x*y;
  //  cout<<temp<<endl;
 Q.pop();
    if(temp>ans)
    {
       ans=temp;
       X=x;
       Y=y;
 }
}
 }
cout<<ans<<endl;
cout<<X<<" "<<Y<<endl;
return 0;
}

No comments:

Post a Comment