Cod sursa(job #1236196)

Utilizator hrazvanHarsan Razvan hrazvan Data 1 octombrie 2014 17:07:37
Problema Carnati Scor 90
Compilator c Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <stdio.h>
#define MAXN 2000
#define INF 2000000000
int t[MAXN + 1], p[MAXN + 1], tab[MAXN + 1], point[MAXN + 1];

inline int max2(int a, int b){
  return a > b ? a : b;
}

void qs(int st, int dr){
  int i = st, j = dr, piv = t[point[(st + dr) / 2]], aux;
  while(i <= j){
    while(t[point[i]] < piv)  i++;
    while(t[point[j]] > piv)  j--;
    if(i <= j){
      aux = point[i];  point[i] = point[j];  point[j] = aux;
      i++;  j--;
    }
  }
  if(st < j)  qs(st, j);
  if(i < dr)  qs(i, dr);
}

int main(){
  FILE *in = fopen("carnati.in", "r");
  int n, c, i;
  fscanf(in, "%d%d", &n, &c);
  for(i = 1; i <= n; i++){
    fscanf(in, "%d%d", &t[i], &p[i]);
    point[i] = i;
  }
  fclose(in);
  qs(1, n);
  int j, cost, max = 0, maxc, plus;
  for(i = 1; i <= n; i++){
    cost = p[point[i]];
    maxc = -INF;
    for(j = 1; j <= n; j++){
      plus = p[point[j]] >= cost ? cost : 0;
      tab[j] = max2(tab[j - 1] + plus - c * (t[point[j]] - t[point[j - 1]]), plus - c);
      if(tab[j] > maxc)  maxc = tab[j];
    }
    if(max < maxc)  max = maxc;
  }
  FILE *out = fopen("carnati.out", "w");
  if(max < 0)  max = 0;
  fprintf(out, "%d", max);
  fclose(out);
  return 0;
}