Cod sursa(job #1724354)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 2 iulie 2016 21:55:37
Problema Gradina Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <bits/stdc++.h>

using namespace std;

const int N_MAX = 256;
const int64_t INF = 0x3f3f3f3f3f3f3f3f;

struct Point {
  int x, y, id;
  bool operator <(const Point &o) const { return (x == o.x ? y < o.y : x < o.x); }
};

int N;
Point P[N_MAX];
bool Who[N_MAX];
string Sol, bestSol;

int64_t Det(const Point &A, const Point &B, const Point &C) {
  return 1LL * (B.x - A.x) * (C.y - A.y) - 1LL * (B.y - A.y) * (C.x - A.x);
}

int64_t getArea(const vector<Point> &P) {
  int64_t Area = 0;
  for(int i = 1; i < P.size(); i++) Area += llabs(Det(P[0], P[i - 1], P[i]));
  return Area;
}

vector<Point> getHull(vector<Point> &P) {
  vector<Point> H;
  int St[N_MAX], Top;
  for(int j = 0; j < 2; j++) {
    St[0] = 0;
    St[1] = 1;
    Top = 1;
    for(int i = 2; i < P.size(); i++) {
      while(Top > 0 && Det(P[St[Top - 1]], P[St[Top]], P[i]) < 0) Top--;
      St[++Top] = i;
    } 
    for(int i = 0; i < Top; i++) H.push_back(P[St[i]]);
    reverse(P.begin(), P.end());
  }
  return H;
}

int64_t getDifference(int fixedFirst, int fixedSecond) {
  vector<Point> lS, rS, lH, rH;
  for(int i = 1; i <= N; i++) {
    if(i == fixedFirst || Det(P[fixedFirst], P[fixedSecond], P[i]) < 0) {
      lS.push_back(P[i]);
      Who[i] = 0;
    } else {
      rS.push_back(P[i]);
      Who[i] = 1;
    }
  }
  lH = getHull(lS);
  if(lS.size() != lH.size()) return INF;
  rH = getHull(rS);
  if(rS.size() != rH.size()) return INF;
  for(int i = 1; i <= N; i++) Sol[P[i].id] = (Who[i] ? 'V' : 'I');
  if(Sol[1] == 'V') for(int i = 1; i <= N; i++) Sol[i] = (Sol[i] == 'V' ? 'I' : 'V');
  return llabs(getArea(lH) - getArea(rH));
}

int main() {
  freopen("gradina.in", "r", stdin);
  freopen("gradina.out", "w", stdout);
  
  scanf("%d", &N);
  for(int i = 1; i <= N; i++) {
    scanf("%d %d", &P[i].x, &P[i].y);
    P[i].id = i;
  }
  
  sort(P+1, P+N+1);

  int64_t minDiff = INF;
  bestSol.assign(N + 1, 'V');
  Sol.assign(N + 1, 'V');
  
  for(int i = 1; i <= N; i++) {
    for(int j = i + 1; j <= N; j++) {
      int64_t Diff = getDifference(i, j);
      if(minDiff > Diff || (minDiff == Diff && bestSol > Sol)) {
        minDiff = Diff;
        bestSol = Sol;
      }
    }
  }
    
  printf("%.1f\n", 0.5 * minDiff);
  for(int i = 1; i <= N; i++) printf("%c", bestSol[i]);
  printf("\n");
  
  return 0;
}