Advertisement
yuawn

algo2017_week5_Building

Oct 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define ALL(o) (o).begin(),(o).end()
  5. #define fo(n) for(int i=0;i<n;i++)
  6. typedef long long LL;
  7.  
  8. bool cmp( int a , int b ) { return a > b; }
  9.  
  10. int main(){
  11.     ios::sync_with_stdio(false);
  12.     cin.tie();
  13.    
  14.     int t;
  15.     cin >> t;
  16.    
  17.     while( t-- ){
  18.         vector<int> a , b;
  19.         int n , tmp;
  20.        
  21.         cin >> n;
  22.         fo( n ) {
  23.             cin >> tmp;
  24.             if( tmp > 0 ) a.pb( tmp );
  25.             else b.pb( tmp * -1 );
  26.         }
  27.        
  28.         sort( ALL(a) , cmp );
  29.         sort( ALL(b) , cmp );
  30.        
  31.         int j = 0 , k = 0 , ans = 1 , now;
  32.         bool p = a[j] > b[k] ? 1 : 0;
  33.        
  34.         now = a[j] > b[k] ? a[j++] : b[k++];
  35.        
  36.         if( !k )
  37.             while( k < b.size() )
  38.                 if( b[k++] < now ){
  39.                     now = b[k - 1] , ++ans , p = 0;
  40.                     break;
  41.                 }
  42.                
  43.         while( ( j < a.size() && !p ) || ( k < b.size() && p ) ){
  44.            
  45.             while( j < a.size() && !p )
  46.                 if( a[j++] < now ) {
  47.                     now = a[j - 1] , p = 1 , ++ans ;
  48.                     break;
  49.                 }
  50.                
  51.             while( k < b.size() && p )
  52.                 if( b[k++] < now ) {
  53.                     now = b[k - 1] , p = 0 , ++ans ;
  54.                     break;
  55.                 }
  56.         }
  57.         cout << ans << endl;
  58.     }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement