Advertisement
bluemmb22

LA3548 Fortune at El Dorado ( two pointer )

Nov 1st, 2016
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1.  
  2. /*
  3.     Problem : http://poj.org/problem?id=2899
  4.     Problem : https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=219&page=show_problem&problem=1549
  5.    
  6.     !!! I couldn't find this solution complexity but I think this is not a good solution !!!
  7.     !!! Try SegmentTree instead : http://pastebin.com/cfQbV1mA
  8. */
  9.  
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <vector>
  13. #include <stdio.h>
  14. #include <set>
  15. using namespace std;
  16. #define lli long long int
  17.  
  18. int x[1005] , y[1005];
  19.  
  20. int main()
  21. {
  22.    
  23.     int T; scanf( "%d" , &T );
  24.     while ( T-- )
  25.     {
  26.         int n , m;
  27.         scanf( "%d %d" , &n , &m );
  28.         int minX = 1005 , maxX = 0;
  29.         int minY = 1005 , maxY = 0;
  30.         for ( int i=0 ; i<n ; i++ )
  31.         {
  32.             scanf( "%d %d" , &y[i] , &x[i] );
  33.             // y[i] = i+1 , x[i] = i+1;
  34.             minY = min( minY , y[i] );
  35.             maxY = max( maxY , y[i] );
  36.             minX = min( minX , x[i] );
  37.             maxX = max( maxX , x[i] );
  38.         }
  39.        
  40.         if ( (n == 0) || (m == 0) )
  41.         {
  42.             printf( "0\n" );
  43.             continue;
  44.         }
  45.        
  46.         int ans = 0;
  47.         {
  48.             vector< pair<int,int> > v;
  49.             for ( int i=0 ; i<n ; i++ ) v.push_back( make_pair( y[i] , x[i] ) );
  50.             sort( v.begin() , v.end() );
  51.            
  52.             int S = min( maxY-minY+1 , m );
  53.             for ( int h=1 ; h<=S ; h++ )        // set fixed width and height and walk on columns
  54.             {
  55.                 if ( (m/h) == (m/(h+1)) ) continue;     // simple and nice optimization !!!
  56.                 int w = m / h;
  57.                    
  58.                 multiset<int> st;
  59.                 for ( int i1=0,i2=0 ; i2<n ; i2++ )
  60.                 {
  61.                     st.insert( v[i2].second );
  62.                     while ( (v[i2].first - v[i1].first) > h )
  63.                     {
  64.                         st.erase( st.find( v[i1].second ) );
  65.                         i1++;
  66.                     }
  67.                    
  68.                     int cnt = 0;
  69.                     for ( multiset<int>::iterator si=st.begin(),sj=st.begin() ; sj!=st.end() ; sj++ )
  70.                     {
  71.                         cnt++;
  72.                         while ( (*sj - *si) > w ) cnt-- , si++;
  73.                         ans = max( ans , cnt );
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.        
  79.         cout<<ans<<"\n";
  80.     }
  81.    
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement