HDU 1394 Minimum Inversion Number (violence/Line tree)

Source: Internet
Author: User

title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1394
violence (TIME:453MS)

#include <stdio.h> #define MAXN 5005int a[maxn];int Main () {    int n,i,j;    while (~SCANF ("%d", &n))    {        int ans=0x3f3f3f3f;        for (i=0; i<n; i++)            scanf ("%d", &a[i]);        int sum=0;        For (i=0, i<n; i++) for            (j=i+1; j<n; j + +)                if (A[i]>a[j])                    sum++;        if (ans>sum)            ans=sum;        for (i=0; i<n; i++)        {            sum=sum-a[i]+n-1-a[i];            if (ans>sum)                ans=sum;        }        printf ("%d\n", ans);    }    return 0;}


Segment Tree (TIMES:93MS)

#include <cstdio> #include <iostream> #include <sstream> #include <cstdlib> #include < cstring> #include <string> #include <climits> #include <cmath> #include <algorithm> #include <queue> #include <vector> #include <stack> #include <set> #include <map>using namespace STD; #define Lson L, M, RT << 1#define Rson m+1, R, RT << 1 | 1#define root 0,n-1,1const int mx=5000+5;int s[mx<<2];void push_up (int rt) {S[rt]=s[rt<<1]+s[rt<<1|1 ];}    void build (int l,int R,int RT) {s[rt]=0;    if (l==r) return;    int m= (L+R) >>1;    Build (Lson); Build (Rson);}        void update (int x,int l,int R,int rt) {if (l==r) {s[rt]=1;    return;    } int m= (L+R) >>1;    if (x<=m) update (X,lson);    else update (X,rson); PUSH_UP (RT);}    int query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) return S[RT];    int m= (L+R) >>1,ret=0;     if (l<=m)   Ret+=query (L,r,lson);    if (r>m) ret+=query (L,r,rson); return ret;}    int main () {int N,X[MX];        while (~SCANF ("%d", &n)) {build (root);        int sum=0;            for (int i=0; i<n; i++) {scanf ("%d", &x[i]);            Sum+=query (X[i],n-1,root);        Update (X[I],ROOT);        } int cnt=sum;            for (int i=0; i<n; i++) {sum=sum-x[i]+n-1-x[i];        if (sum<cnt) cnt=sum;    } printf ("%d\n", CNT); } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1394 Minimum Inversion Number (violence/Line tree)

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.