HDU 1394 Minimum Inversion Number (segment tree single point update)

Source: Internet
Author: User

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

Test instructions: There is a sequence of 0~n-1 numbers, and then this operation, each time the first element is placed to the last surface to get a sequence, each get a sequence can be derived from the sequence of the number of reverse order (if the number of the previous and the opposite of the size sequence, that is, the preceding number is greater than the following number, they are called a reverse . The total number of reverse order in a permutation is called the number of reverse order in this arrangement. Required to find the smallest number of reverse order.

Train of Thought: (has been doing the line tree problem, see the list has this problem.) Think for a long time not to see the problem-solving report made out. )

Say what you think:
1, for a certain sequence, one of the number of a[i] can constitute the number of reverse order, only to determine in the range of A[i]+1~n to find whether the number of times before the occurrence;
2, then find out the reverse number of the first sequence.
3, by the first sequence of the reverse number can be introduced its next sequence of reverse number. The inverse number of the next sequence is regular sum = the previous Sum-a[i]-1-a[i]; If you come up with something like:

Sequence 3 6 9 0 8 5 7 4 2 1 Move 3 to the back, then it will reduce the number of reverse order by 3 (0 2 1) but also increase n-a[i]-1.

How many times do you ask for it to occur in 1? Specifically, the update function principle is that, from the beginning of the update, each loss of a number in its corresponding position to mark its occurrence. If you lose 3 6 9 then node[u].l = NODE[U].R = 3 6 9 The sum is 1 when input 0 o'clock, find out what number of input before the interval 1~10 is its inverse number and then 3 6 9 have appeared.
#include <stdio.h> #include <iostream> #include <string.h>using namespace std; #define Manx 5005struct Tree{int left;int right;int num;} Tree[4 * manx];int a[manx];void buildtree (int i, int l, int r), void Update (int i, int id), int Query (int i, int l, int r); NT main () {int n;while (~scanf ("%d", &n)) {int ans = 0; Buildtree (1, 1, n); for (int i = 0; i < n; i++) {scanf ("%d", &a[i]); Update (1, a[i] + 1), ans + = Query (1, A[i] + 2, N),//printf ("ans =%d\n", ans); int m = ans;for (int i = 0; i < n; i++) {m = m + n-2 * A[i]-1;if (M < ans) ans = m;} printf ("%d\n", ans);} return 0;} void Buildtree (int i, int l, int r) {tree[i].left = L;tree[i].right = R;tree[i].num = 0;if (Tree[i].left = = Tree[i].right) r Eturn;int mid = (L + r) >> 1; Buildtree (i << 1, L, mid); Buildtree (i << 1 | 1, mid + 1, r);} void Update (int i, int id) {if (Tree[i].left = = id && tree[i].right = = id) {tree[i].num = 1;return;} int mid = (tree[i].left + tree[i].right) >> 1;if (ID > mid) Update (i << 1 | 1, ID); Else update (i << 1, id); tree[i].num = tree[i << 1].num + tree[i << 1 | 1].num;} int Query (int i, int l, int r) {//printf ("%d%d%d\n", I, L, R), if (L > R) return 0;if (Tree[i].left = = L && tree[ I].right = = R) Return tree[i].num;int mid = (tree[i].left + tree[i].right) >> 1;if (R <= Mid) return Query (i <& Lt 1, L, R), else if (L > Mid) return query (I << 1 | 1, L, R), else return query (I << 1, L, mid) + query (i &LT;&L T 1 | 1, Mid + 1, r);}

  

HDU 1394 Minimum Inversion Number (segment tree single point update)

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.