Cod sursa(job #1217927)

Utilizator toranagahVlad Badelita toranagah Data 8 august 2014 20:08:52
Problema Avioane Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
//Problem avioane from Infoarena
#include <cmath>
#include <cstdint>
#include <algorithm>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

ifstream fin("avioane.in");
ofstream fout("avioane.out");

const int64_t INF = 1LL << 60;
const int MAX_N = 100100;

int N;
int v[MAX_N];
int64_t result = -INF;

inline int get_b(int e1, int e2) {
  if (v[e1] == v[e2]) return (1 << 30);
  return floor((1.0 * (e1 * v[e1] - e2 * v[e2])) / (1.0 * v[e1] - v[e2]));
}

inline int64_t calc(int e, int b) {
  return 1LL * (b - e) * v[e] + 1LL * (N - b + 1) * v[b];
}

void match_val(int klo, int khi, int vlo, int vhi);

int main() {
  fin >> N;
  for (int i = 1; i <= N; i += 1) {
    fin >> v[i];
  }

  sort(v + 1, v + N + 1);
  match_val(1, N, 1, N);

  fout << result;
  return 0;
}

void match_val(int klo, int khi, int vlo, int vhi) {
  int kmid = klo + (khi - klo) / 2;

  int64_t best = -INF;
  int vbest = vlo;
  for (int i = vlo; i <= vhi; i += 1) {
    int now = calc(i, kmid);
    if (now > best) {
      best = now;
      vbest = i;
    }
  }
  result = max(result, best);

  if (klo == khi) return;
  match_val(klo, kmid, vlo, vbest);
  match_val(kmid + 1, khi, vbest, vhi);

}