Segment Tree-Minimum reverse order number hdu1394

Source: Internet
Author: User

Title: Segment Tree-minimum number of reverse order
Date:2018-10-12 17:19:16
Tags

    • Acm
    • Algorithm
    • Brush questions
      Categories
    • acm-segment Tree
Overview

This is a simple line-of-tree problem, and, of course, there are many other practices, and even when violence can be,,,

The line tree is mainly used to practice a line tree, and this time, I changed a way of writing line-segment trees,

Seems to be a lot of big guys are using a kind of writing,,,

A way of getting started with a good understanding: nodes are represented by a struct node, and for the sake of understanding the left and right borders of each node are added,

But in fact, this information is useless,,, or superfluous,, directly in the use of the calculation or directly as a function of the formal parameters of the line,,,,

This way of writing code is less and more convenient to write, and occupy less space,,,

Analysis of the topic

This problem is not the same as the line tree before the value of the maintenance of the answer,,, but the middle of a certain process volume,,

First, the title means that for a given sequence \ (A_0, a_1, a_2,,,,, a_{n-1}\) ,,, each time the first number is moved to the back, so that a total of n sequences ,,, and then for each sequence there is an inverse number , asking you how small the smallest one in these inverse numbers is,,,

    • This problem as long as you know one of the sequence of the reverse number, its adjacent an inverse number can be pushed out,, specifically:

      \ (When the reverse number of the sequence of sum_i is known, \)

      \ (ordinal number of i+1 sequence is Sum_{i+1}=sum_i + n-a[i]-1-a[i],,,, \)

      \ (that is, when the first number is moved to the last,,, \)

      \ (It has a[i in the previous reverse order), so subtract these, \)

      \ (And when it is moved to the last,,, \)

      \ (more n-a[i in front)-1,,, \)

      \ (The last sum is out,,, \)

    • When the above recursion is known, our task is to find out the number of reverse order of the input sequence, and then, based on the recursive type, the smallest output is OK,
    • The way to find the inverse number of this sequence with a line tree is,,, first build an empty number, and then each input a number, mark it, but the mark in the last update is complete,, first of all the number of inputs before it is larger than its number (that is, see this number to n-1 a total of several appear in the previous input, , that is, to look at the Mark's and),, that is, the inverse sequence of it, and then add (mark) it to this tree (update), and, as you can see, if you change the tag to hold this number, and wonder if the tree's leaf node is a sort of 1~n-1 sequence,,, this section of the picture is good understanding

Realize

Code

#include <iostream> #include <cstdio> #include <cstdlib>using namespace std; #define Lson Rt<<1, L,mid#define rson rt<<1|1,mid+1,rconst int maxn = 5005;int sum[maxn << 2];void pushup (int rt) {Sum[rt] = su M[rt << 1] + sum[rt << 1 | 1];}    void build (int rt, int l, int r) {Sum[rt] = 0;    if (L = = r) return;    int mid = (L + r) >> 1;    Build (Lson);    Build (Rson); Pushup (RT);}        void update (int rt, int l, int r, int loc) {if (L = = r) {++SUM[RT];    Return    } int mid = (L + r) >> 1;    if (Loc <= mid) Update (Lson, loc);    else Update (Rson, loc); Pushup (RT);}    int query (int rt, int l, int r, int l, int r) {if (L <= l && R <= R) return SUM[RT];    int mid = (L + r) >> 1;    int ans = 0;    if (L <= mid) ans + = query (Lson, L, R);    if (R > mid) ans + = query (Rson, L, R); return ans;}    int A[maxn];int Main () {int n; while (scanf("%d", &n)! = EOF) {build (1, 0, N);        int sm = 0;            for (int i = 0; i < n; ++i) {scanf ("%d", &a[i]);            SM + = query (1, 0, n-1, A[i], n-1);        Update (1, 0, n-1, A[i]);        } int ret = SM;            for (int i = 0; i < n; ++i) {SM + = N-a[i]-1-a[i];        ret = min (SM, ret);    } printf ("%d\n", ret); }}

Segment Tree-minimum number of reverse order hdu1394

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.