【CF1324D】Pair of Topics

  The meaning of problems: Given a sequence of length n-$ $ is obtained satisfies $ 1 \ le i <j \ le n $ and $ a_i + a_j> b_i + b_j $ a $ (i, j) $ log.


  We put $ a_i + a_j> b_i + b_j $ deformed into $ (a_i-b_i) + (a_j-b_j)> 0 $.

  Order $ c_i = a_i-b_i \; (1 \ le i \ le n) $, then requires only $ c_i + c_j> 0 $ a.

  For $ 1 $ to $ n $ each $ i $, we have found to meet the $ c_j> number -c_i $ of $ j $, and the statistics add up, which can be used to sort + STL or half solved.

  Because for $ i \ neq j $ time, $ (i, j) $ and $ (j, i) $ certainly are not met. $ I <j $, so the answer has figured out exactly half that does not meet the $ i < j $, and divided by $ 2 $.

  Time complexity $ O (n \; \ log \; n) $.


  In the statistics, we find that the statistics did not meet the $ j $ $ j \ neq i $.

  If the current $ i $ meet $ 2c_i> 0 $, ie $ c_i> 0 $, he was definitely going to be counted.

  So the answer is subtracted from the value of statistics to meet the $ c_i> number $ i $ 0 $, divided by $ 2 $.

  Reference Code:

#include <algorithm>
#include <cstdio>
#define INF 1e9
#define eps 1e-6
typedef long long ll;

int n, a[200010], b[200010], k;
ANS LL;                                 // note open long long

int main () {

    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = 1, x; i <= n; i++)
        Scanf ( " % D " , & X), A [I] - = X;     // here is equivalent to the above a_i C_i 
    
    STD :: Sort (A + . 1 , + n-A + . 1 );
     for ( int I = . 1 ; I <= n-; I ++ ) {
        K = STD :: upper_bound, (A + . 1 , + n-A + . 1 , -a [I]) - A;    
         // seeking above is greater than the first number of position -a_i 
        IF (A [I]> 0 ) ans--;             // Laid determination a_i> 0 
        ANS + = (LL) n-- K + . 1 ;
    }
    
    printf ( " % LLD \ the n- " , ANS >> 1 );         // answers divided by 2

    return 0;
}

Guess you like

Origin www.cnblogs.com/zengpeichen/p/12484976.html