HDU 1394 minimum inversion number (reverse Number of Line Segment trees)

Source: Internet
Author: User

Address: HDU 1394

You can use the line segment tree to calculate the number of reverse orders.

The maintenance information of this question indicates whether each number has been displayed. After each input, the query is performed from the value of the point to n-1. Each time a number is found, because it is found after the number, this number must be larger than this number. That is, a pair of reverse orders, and then the number of reverse orders + 1. After finding all the reverse orders, the rest can be recursive. For example, if the first number is X, then when we put it at the end, the less reverse number is the number smaller than the first number. The number of multiple reverse orders is the number of numbers that are larger than the number that is placed before the end. Because all numbers are from 0 to n-1. So the number smaller than him is X, the number larger than him is the n-1-x. In this way, the number of reverse orders can be calculated using the O (1) time. Then you can find the minimum value.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <cstring> # include <math. h >#include <stack> using namespace STD; # define lson L, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1 const int maxn = 6e3; int sum [maxn <2], a [maxn]; void pushup (int rt) {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void build (int l, int R, int RT) {sum [RT] = 0; If (L = r) {return;} int mid = L + r> 1; build (lson); Build (rson );} void Update (int p, int L, int R, int RT) {If (L = r) {sum [RT] ++; return;} int mid = L + r> 1; if (P <= mid) Update (p, lson); else Update (p, rson); pushup (RT);} int query (int ll, int RR, int l, int R, int RT) {If (LL <= L & RR> = r) {return sum [RT];} int mid = L + r> 1; int ans = 0; If (LL <= mid) ans + = query (LL, RR, lson); If (RR> mid) ans + = query (LL, RR, rson); Return ans;} int main () {int N, I, ANS, Y; while (scanf ("% d", & N )! = EOF) {build (0, n-1, 1); ans = 0; for (I = 0; I <n; I ++) {scanf ("% d ", & A [I]); ans + = query (A [I], n-1, 0, n-1, 1); Update (A [I], 0, n-1, 1);} y = ans; for (I = 0; I <n; I ++) {Y + = n-1-2 * A [I]; ans = min (ANS, y);} printf ("% d \ n", ANS);} return 0 ;}


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.