[HDOJ1394] Minimum inversion Number (segment tree, reverse order)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1394

Test instructions: The inverse number of a number is inv, and then the number of the first number of this sequence is changed to the last digit, the reverse of the number of reverse order in the process.

Using a line segment tree, first create an empty tree, and then insert points in the tree. Make a statistic before inserting, and find the number that is larger than this one, and add together. The current number of reverse order is obtained. You can also merge methods such as sorting, tree arrays, and so on.

After moving the first number of each time, you can use two to search two times, to find out how many larger than it, how many smaller than it. With the number of the current reverse order plus a number larger than it, minus the number smaller than it can be the current number of reverse.

You can also get the rule: when the number of I becomes the number of n-1, subtract the smaller number (Tmp[i]), plus the number (n-tmp[i]+1) that is larger than the one behind it.

1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <cstdlib> One#include <bitset> A#include <vector> -#include <deque> -#include <queue> the#include <stack> -#include <ctime> -#include <Set> -#include <map> +#include <cmath> -  + using namespacestd; A  at #defineLson L, M, RT << 1 - #defineRson m + 1, R, RT << 1 | 1 - Const intMAXN =6666; - intnum[maxn<<2]; - intTMP[MAXN]; -  in voidPushup (intRT) { -NUM[RT] = num[rt<<1] + num[rt<<1|1]; to } +  - voidBuildintLintRintRT) { theNUM[RT] =0; *     if(L = =r) { $         return;Panax Notoginseng     } -     intm = (L + r) >>1; the build (Lson); + build (Rson); A } the  + voidUpdateintPintLintRintRT) { -     if(L = =r) { $num[rt]++; $         return; -     } -     intm = (L + r) >>1; the     if(P <=m) { - Update (P, Lson);Wuyi     } the     Else { - Update (P, Rson); Wu     } - pushup (RT); About } $  - intQueryintLintRintLintRintRT) { -     if(l <= l && R <=R) { -         returnNum[rt]; A     } +     intm = (L + r) >>1; the     intCur =0; -     if(L <=m) { $Cur + =query (L, R, Lson); the     } the     if(R >m) { theCur + =query (L, R, Rson); the     } -     returncur; in } the  the intMain () { About     //freopen ("in", "R", stdin); the     intN; the      while(~SCANF ("%d", &N)) { theBuild0, N-1,1); +         intINV =0; -          for(inti =0; I < n; i++) { thescanf"%d", &tmp[i]);BayiINV + = query (Tmp[i], N-1,0, N-1,1); theUpdate (Tmp[i],0, N-1,1); the         } -         intAns =0; -          for(inti =0; I < n; i++) { theINV = Inv-tmp[i] + n-tmp[i]-1; theAns =min (ans, inv); the         } theprintf"%d\n", ans); -     } the     return 0; the}

[HDOJ1394] Minimum inversion Number (segment tree, reverse order)

Related Article

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.