2016 Summer Camp Training 2 H q frosh Week

Source: Internet
Author: User

The silly question of finding the number of reverse order.

Use a merge sort or tree array or line segment tree

Merge sort code:

# include<stdio.h># define MAXN 500100int a[maxn],b[maxn];long long ans;void merge (int l, int r) {int k, p, Q;    if (L = = r) return;    int mid= (l+r) >>1;    Merge (L,mid);    Merge (mid+1,r);    p = l; Q = mid+1; k = l;    while (P<=mid | | q<=r) {        if (P>mid | | (Q<=r && a[p]>a[q])) {            b[k++]=a[q++];            ans+=mid-p+1;        }        else             b[k++]=a[p++];    }    for (k=l; k<=r; k++) a[k] = b[k];    return;} int main () {    int n;    while (~SCANF ("%d", &n)) {ans = 0;        for (int i=0;i<n;i++)            scanf ("%d", a+i);        Merge (0,n-1);        printf ("%i64d\n", ans);    }    return 0;}

  

2016 Summer Camp Training 2 H q frosh Week

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.