1688 reverse order, 1688 Reverse Order

Source: Internet
Author: User

1688 reverse order, 1688 Reverse Order
1688 Reverse Order

 

Time Limit: 1 s space limit: 128000 KB title level: Gold Title Description Description

Given a sequence a1, a2 ,..., An. If I <j and ai> aj exists, we call it a reverse order pair to find the number of Reverse Order pairs.

 

Data range: N <= 105. Ai <= 105. The time limit is 1 s.

 

Input description Input Description

The first behavior n indicates the sequence length. The next n rows indicate the number of I in the sequence.

Output description Output Description

Total number of all reverse order pairs.

Sample Input Sample Input

4

3

2

3

2

Sample output Sample Output

3

Data range and prompt Data Size & Hint 
1 # include <iostream> 2 # include <cstring> 3 using namespace std; 4 long int a [1000001]; 5 long int tot; 6 long int n; 7 long int ans [1000001]; // storage result 8 long int now; // It indicates how many results 9 void f (long int s, long int t) 10 {11 // int mid, I, j, k; 12 if (s = t) return; 13 int mid = (s + t) /2; 14 f (s, mid); 15 f (mid + 1, t); 16 long int I = s; 17 long int j = mid + 1; 18 now = s; 19 while (I <= mid & j <= t) 20 {21 if (a [I] <= a [j]) 22 {23 // tot ++; 24 25 ans [now] = a [I]; 26 I ++; 27 now ++; 28} 29 else30 {31 tot = tot + mid-I + 1; 32 ans [now] = a [j]; 33 j ++; 34 now ++; 35} 36} 37 while (I <= mid) 38 {39 ans [now] = a [I]; 40 I ++; 41 now ++; 42} 43 while (j <= t) 44 {45 ans [now] = a [j]; 46 j ++; 47 now ++; 48} 49 for (I = s; I <= t; I ++) // put the merged ordered data back into array a 50 a [I] = ans [I]; 51 52} 53 int main () 54 {55 long int n; 56 cin> n; 57 for (int I = 1; I <= n; I ++) 58 cin> a [I]; 59 f (1, n ); 60 cout <tot; 61/* for (int I = 1; I <= n; I ++) 62 {63 cout <a [I] <""; 64} */65 return 0; 66}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.