Cod sursa(job #826528)

Utilizator BitOneSAlexandru BitOne Data 30 noiembrie 2012 20:47:27
Problema Avioane Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;

const int NMAX = 100011;

vector< int > v;
vector< int > w[NMAX];

inline int getIndex(int i, int maxIndex)
{
   if(v[i] <= v[maxIndex]) return NMAX - 1;
   long long index = 1 + (1LL * v[i] * i - maxIndex) / (v[i] - v[maxIndex]);
   return index >= NMAX ? NMAX - 1 : index;
}
inline int _max(int x, int y) {return x >= y ? x : y;}
int main()
{
   long long profit;
   int N, M, x, maxIndex, i, j;
   ifstream in("avioane.in");
   ofstream out("avioane.out");

   in >> N;
   profit = 0;
   switch(N)
   {
      case 2  : in >> profit;

      case 1  : in >> x;
                profit+=x;
                out << profit << '\n';
                return EXIT_SUCCESS;

      default : copy(istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v));
   }

   sort(v.begin(), v.end());
   profit = 1LL * v[0] * N;
   maxIndex = 0;
   for(i = 1; i < N; ++i)
   {
      if(false == w[i].empty())
      {
          M = w[i].size();
          for(j = 0; j < M; ++j)
          {
              if(1LL * v[maxIndex] * (i - maxIndex) < 1LL * v[w[i][j]] * (i - w[i][j]))
              {
                 maxIndex = w[i][j];
              }
          }
          for(j = 0; j < M; ++j)
          {
              w[getIndex(w[i][j], maxIndex)].push_back(w[i][j]);
          }
      }
      profit = _max(profit, 1LL * v[i] * (N - i) + 1LL * v[maxIndex] * (i - maxIndex));
      if(1LL * v[maxIndex] * (i - maxIndex + 1) < v[i])
      {
         maxIndex = i;
      }
      else w[getIndex(i, maxIndex)].push_back(i);
   }
   out << profit << '\n';

   return EXIT_SUCCESS;
}