Hdoj 1394 minimum inversion number (Reverse Order Number pair)

Source: Internet
Author: User

~~~ Question Link ~~~

A sequence is given, and it is required to find the sequence and the smallest in the reverse order number pair after it passes through the ring shift.

Idea; use the line segment tree to calculate the number of reverse orders. Whenever a number X is added, X ~ is obtained ~ N-1 indicates the number of Reverse Order pairs.

After a set of sequences are obtained, the inverse number pair after the ring shift can be obtained using the formula, and the ring movement is implemented using STL.

Code:

# Include <stdio. h> # include <string. h >#include <algorithm> # define n 5002 using namespace STD; int T [4 * n]; void Update (int c, int L, int R, int W) {int M = L + (R-l)/2; If (L = r) {T [c] = 1; return;} If (W <= m) update (2 * C, L, M, W); else update (2 * C + 1, m + 1, R, W ); T [c] = T [2 * C] + T [2 * C + 1];} int query (int c, int L, int R, int lf, int RT) {int sum = 0, M = L + (R-l)/2; If (LF <= L & RT> = r) return T [c]; if (LF <= m) S Um + = query (2 * C, L, M, lf, RT); If (RT> m) sum + = query (2 * C + 1, m + 1, r, lf, RT); Return sum;} int main () {int I = 0, n = 0, sum = 0, min = 0, num [N]; while (scanf ("% d", & N )! = EOF) {sum = 0; memset (T, 0, sizeof (t); for (I = 0; I <n; I ++) {scanf ("% d", & num [I]); sum + = query (1, 0, n-1, num [I], n-1); Update (1, 0, 0, n-1, num [I]);} min = sum; for (I = 0; I <n-1; I ++) {sum = sum-num [0] + (n-1) -num [0]; rotate (Num, num + 1, num + n); If (min> sum) min = sum;} printf ("% d \ n ", min);} 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.