Cod sursa(job #1694615)

Utilizator TincaMateiTinca Matei TincaMatei Data 25 aprilie 2016 17:46:42
Problema Cutii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 2.27 kb
#include <stdio.h>
#define MAX_CUTII 3500

struct Cutie {
  int x , y , z;
}cutii[ MAX_CUTII ];

int aib[ 1 + MAX_CUTII ][ 1 + MAX_CUTII ];

int lastBit( int x ) {
  return ( x ^ ( x - 1 ) ) & x;
}

int max( int a , int b ) {
  if( b > a )
    a = b;
  return a;
}

void addAt( int l , int c , int n , int val ) {
  int cCopie;
  cCopie = c;
  while( l <= n ) {
    c = cCopie;
    while( c <= n ) {
      aib[ l ][ c ] = max( aib[ l ][ c ] , val );
      c += lastBit( c );
    }
    l += lastBit( l );
  }
}

void removeAib( int l , int c , int n ) {
  int cCopie;
  cCopie = c;
  while( l <= n ) {
    c = cCopie;
    while( c <= n ) {
      aib[ l ][ c ] = 0;
      c += lastBit( c );
    }
    l += lastBit( l );
  }
}

int query( int l , int c ) {
  int rez , cCopie;
  rez = 0;
  cCopie = c;
  while( l >= 1 ) {
    c = cCopie;
    while( c >= 1 ) {
      rez = max( aib[ l ][ c ] , rez );
      c -= lastBit( c );
    }
    l -= lastBit( l );
  }
  return rez;
}

void qSort( int begin , int end ) {
  int b , e , pivot;
  struct Cutie aux;
  b = begin;
  e = end;
  pivot = cutii[ ( b + e ) / 2 ].x;
  while( b <= e ) {
    while( cutii[ b ].x < pivot ) b++;
    while( cutii[ e ].x > pivot ) e--;
    if( b <= e ) {
      aux = cutii[ b ];
      cutii[ b ] = cutii[ e ];
      cutii[ e ] = aux;
      b++;
      e--;
    }
  }
  if( b < end )
    qSort( b , end );
  if( begin < e )
    qSort( begin , e );
}

void solveTest( FILE *fin , FILE *fout , int n ) {
  int i , max , rez;
  for( i = 0 ; i < n ; i++ )
    fscanf( fin , "%d%d%d" , &cutii[ i ].x , &cutii[ i ].y , &cutii[ i ].z );
  qSort( 0 , n - 1 );
  max = 0;
  for( i = 0 ; i < n ; i++ ) {
    rez = query( cutii[ i ].y - 1 , cutii[ i ].z - 1 );
    addAt( cutii[ i ].y , cutii[ i ].z , n , rez + 1 );
    if( rez + 1 > max )
      max = rez + 1;
  }

  for( i = 0 ; i < n ; i++ )
    removeAib( cutii[ i ].y , cutii[ i ].z , n );

  fprintf( fout , "%d\n" , max );
}

int main() {
  int n , t , i;
  FILE *fin = fopen( "cutii.in" , "r" );
  fscanf( fin , "%d%d" , &n , &t );

  FILE *fout = fopen( "cutii.out" , "w" );

  for( i = 0 ; i < t ; i++ )
    solveTest( fin , fout , n );

  fclose( fin );
  fclose( fout );
  return 0;
}