#1141: Two-point and merge-sorted inverse pairs (merge sort)

Source: Internet
Author: User


#1141: Two-point and merge-sort in reverse order
Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB

Describe

We knew Nettle was playing "Battleship これ" on the previous, last, and upper back. After a bitter struggle, nettle again got a lot of boats.
This day nettle is checking its fleet list:
[List.png]
As we can see, the ship's default sort is the rank parameter. But in fact a ship's fire value and the level of the relationship is not big, so there will be a ship than B ship grade, but a ship fire is lower than the case of B ship. For example, the 77-stage dragon to two firepower is less than the 55-level of the XI-Li two.
Now that the nettle will give all the ship's firepower in order of rank, please calculate how many pairs of ships meet the above mentioned situation.

Tip: High firepower is fierce!
Input

Line 1th: an integer n. N indicates the number of ships, 1≤n≤100,000
Line 2nd: n integers, the number of I indicates the fire value of the ship with low rank a[i],1≤a[i]≤2^31-1.
Output

Line 1th: An integer that indicates how many pairs of ships meet "a ship is higher than B ship, but a ship's firepower is lower than ship B."
Sample input

10
1559614248 709366232 500801802 128741032 1669935692 1993231896 369000208 381379206 962247088 237855491

Sample output

27

#include <cstdio> #include <algorithm>//#include <bits/stdc++.h>using namespace std;template<    Class T>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=100011;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) TypEdef Long Long ll;typedef double db;typedef pair<int,int> P; #define BUG printf ("---\ n"); #define MOD 10007LL ans=0;/        /int is not an existing void Merge_sort (int *a,int x,int y,int *t) {if (y-x>1) {int p=x,m= (x+y)/2;        int i=x;        int q=m;        Merge_sort (a,x,m,t);        Merge_sort (a,m,y,t); while (p<m| | Q<y) {if (q>=y| | (P<m&&a[p]<=a[q]))            T[i++]=a[p++];        else {t[i++]=a[q++];ans+=m-p;}    } for (int i=x;i<y;i++) a[i]=t[i]; }} int A[MAXN];    int B[maxn];int Main () {//#ifndef Online_judge//Freopen ("In.txt", "R", stdin);    #endif//Online_judge int n,m,i,j,k,t;        while (read (n)) {for (i,0,n) {read (a[i]);        } ans=0;        Merge_sort (A,0,N,B);    Writeln (ANS); } return 0;}



#1141: Two-point and merge-sorted inverse pairs (merge sort)

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.