Cod sursa(job #300495)

Utilizator alecmanAchim Ioan Alexandru alecman Data 7 aprilie 2009 14:34:14
Problema Carnati Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include<cstdio>
#include<algorithm>

using namespace std;

#define INPUT "carnati.in"
#define OUTPUT "carnati.out"

const int NMAX = 2001;

FILE *fin = fopen(INPUT, "r"), *fout = fopen(OUTPUT, "w");

long N, M;
long T[ NMAX ], C[ NMAX ], P[ NMAX ];

void readData()
{
  fscanf(fin, "%ld %ld", &N, &M);
  
  for(long i = 1; i <= N; ++i)
  {
    fscanf(fin, "%ld %ld", &T[ i ], &C[ i ]);
    P[ i ] = i;
  }
}

inline int cmp(long X, long Y)
{
  return T[ X ] < T[ Y ];
}

void solve()
{
  long profMax1 = 0, profMax2 = 0;
  long contSt = 0, contDr = 0, conL, conR;
  long prof = 0;
  long maxFin = 0;
  long pozL, pozR;
  
  for(long i = 1; i <= N; ++i)
  {
    profMax1 = profMax2 = 0;
    pozL = pozR = 0;
    contSt = contDr = 0;
    conL = conR = 0;
    
    for(long j = i-1; j > 0; --j)
    {
      prof = 0;
      
      if(C[ P[j] ] >= C[ P[i] ])
        ++contSt;

      prof = C[ P[i] ] * contSt - (T[ P[i] ]-T[ P[j] ]+1)*M;
      //if(i == 4) fprintf(stderr, "%ld %ld ", prof, cont);
      
      if(prof > profMax1) profMax1 = prof, pozL = j, conL = contSt;
    }
    
    //if(i==4) fprintf(stderr, "\nprof=%ld\n", profMax1);
    
    for(long j = i+1; j <= N; ++j)
    {
      prof = 0;
      
      if(C[ P[j] ] >= C[ P[i] ])
        ++contDr;

      prof = C[ P[i] ] * contDr - (T[ P[j] ]-T[ P[i] ]+1)*M;
      //if(i == 4) fprintf(stderr, "%ld %ld ", prof, contDr);
      
      if(prof > profMax2) profMax2 = prof, pozR = j, conR = contDr;
    }
    
    //prof = profMax1 + profMax2;
    //if(i==4) fprintf(stderr, "%ld %ld\n", conL, conR);
    prof = C[ P[ i ] ] * (conL + conR +1) - (T[ P[ pozR ] ] - T[ P[ pozL ] ] + 1) * M;
    
    if(prof > maxFin) maxFin = prof;
    
    //fprintf(stderr, "i=%ld prof=%ld\n", i, prof);
  }
  
  fprintf(fout, "%ld\n", maxFin);
}

int main()
{
  readData();
  
  sort(P+1, P+N+1, cmp);
  
  solve();
  
  fclose(fin);
  fclose(fout);
  
  return 0;
}