HDU 1394 minimum inversion number (number of reverse orders for Single Point update of a line segment tree)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394

Here is a sequence of N numbers, which consists of only 0-n. We can perform the following operation: Move the first number to the last one, with no limit on the number of times. Q: What is the minimum number of reverse orders in the original sequence and the latest sequence?


At first, I thought we needed to enumerate and calculate the number of reverse orders, but at last we knew that this question was regular:A series of N numbers composed of 0-n. When the first number is moved to the last bit, the reverse Number of the entire series will decrease X [I] (before moving, n-X [I]-1 (after moving, the front is greater than him ).

Then this question is simply the reverse number of the original series.

Previously, the number of reverse orders was merged and the line segment tree was used. Unfortunately, it was not summarized. At that time, the questions still needed to be discretization. Now let's sum up this simple one. It will happen when we continue to review the summer vacation content.

Reverse Number of Line Segment trees

Because the line segment tree is good at solving the range problem, we can use it to quickly sum the range. We include each number in the line segment tree. Before a new number is input, count the number already entered, and so on. The final sum is the reverse order.


Code:

# Include <iostream> # include <cstdio> # include <algorithm> # define ls RT <1 # define Rs RT <1 | 1 # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define mid (L + r)> 1 # define Max 5000 using namespace STD; int num [max <2]; int X [Max + 10]; inline void pushup (int rt) {num [RT] = num [ls] + num [RS];} void build (int l, int R, int RT) {num [RT] = 0; if (L = r) return; int M = mid; build (lson); Build (R Son);} void Update (int p, int L, int R, int RT) {If (L = r) {num [RT] ++; return ;} int M = mid; If (P <= m) Update (p, lson); else Update (p, rson); pushup (RT);} int query (INT Ql, int QR, int L, int R, int RT) {If (L> = QL & R <= QR) {return num [RT] ;} int M = mid; int ret = 0; If (QL <= m) RET + = query (QL, Qr, lson); If (QR> m) RET + = query (QL, Qr, rson); return ret;} int main () {int N; while (~ Scanf ("% d", & N) {int sum = 0; build (0, N-1,1); For (INT I = 0; I <N; I ++) {scanf ("% d", & X [I]); sum + = query (X [I], n-, N ); update (X [I], 0, n-1, 1);} int ans = sum; For (INT I = 0; I <n; I ++) {// 0-N number, move the first number to the last digit and then // The Reverse Number of the entire series will decrease X [I] (before moving, the latter is smaller than him) // n-X [I]-1 will be added (after moving, the sum + = N-X [I]-X [I]-1 will be larger than the former; ans = min (ANS, sum);} printf ("% d \ n", ANS);} return 0 ;}


HDU 1394 minimum inversion number (number of reverse orders for Single Point update of a line segment 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.