HDU -- 4911 -- merge sort | tree Array

Source: Internet
Author: User

I found a lot of things in a small reverse order.

To solve this problem, you need to know something...

I can write a string of given numbers at will ..

Index: 0 1 2 3 4

Value: 4 8 7 5 6 at this time, the total reverse logarithm is 3 + 2 = 5. If we can only exchange adjacent elements, what is the best case?

It must be assumed that arr [x]> arr [x + 1] forms a reverse order number. Let's exchange it so there is no reverse order number, right?...

But it does not affect other elements, right?

So the minimum number of times we need is sum <that is, the number of reverse orders of the original array> and then compare it with K.

The first time I saw the post in disucss, I realized that sort is an unstable sorting... stable_sort should be used in this question .. Writing without tree Arrays

I am so tired to play with mhxy and LOL EVERY DAY =-=

 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4  5 typedef long long LL; 6 LL sum; 7 const int size = 100010; 8 int arr[size]; 9 int temp[size];10 11 void merge_sort( int l , int r )12 {13     int mid , p , q , i;14     if( r-l > 1 )15     {16         mid = l + (r-l) / 2;17         p = l;18         q = mid;19         i = l;20         merge_sort( l , mid );21         merge_sort( mid , r );22         while( p<mid || q<r )23         {24             if( q>=r || (p<mid && arr[p] <= arr[q] ) )25             {26                 temp[i++] = arr[p++];27             }28             else29             {30                 temp[i++] = arr[q++];31                 sum += (mid-p);32             }33         }34         for( i = l ; i<r ; i++ )35         {36             arr[i] = temp[i];37         }38     }39 }40 41 int main()42 {43     cin.sync_with_stdio(false);44     int n;45     LL k;46     while( cin >> n >> k )47     {48         sum = 0;49         for( int i = 0 ; i<n ; i++ )50         {51             cin >> arr[i];52         }53         merge_sort( 0 , n );54         cout << max( sum-k , (LL)0 ) << endl;55     }56     return 0;57 }
View code

In this case, you must use ll to forcibly convert the values above 0. Otherwise, CE will be performed because the former is of the LL type ..

It is easy to use 64-bit when doing the reverse order of the number of pairs, because if you give such a group of Bt array X, X-1, X-2, X-3 ,............ X-I, x-i-1 ,............, 1 is the sum of the ah equidifference Series #24 ..

Leave a large blank area for the tree array to be written ....

I suddenly want to write something about the Merge Sorting. <I think it is very difficult to merge. I am most annoyed with this recursive slot .. envy those who write recursion and write .. #80>

It seems that the grouping operation of Merge Sorting keeps binary operations and finally ends recursion when only one element is split.

Then we start to step by step from the ending Recursive Function and slowly go back to the upward operation. Maybe we will merge the four elements 2, 4, and 3. So we will do this.

<,> ---> <>, <> ----> <2.>, <1.> <4, 3> ----> <4, >,< 3>

First, we need to compare the relationship between 2 and 1. If 1 is less than 2, we need to copy the array in the right space. Here, we use mid as the demarcation line. If we want to perform the counting operation of the number of logarithm in the reverse order in this case, we need to use mid-P to calculate the total number of data in the left space. You don't have to worry about Repeated Computation. Because when all the space on the left and right is traversed, it will merge the data and calculate it together. this is one of the larger left/right intervals.

-------- Well, it seems that you can think of these places for the time being. There may be errors ..

I had to vomit about 6 or 7 times when I wrote down the above items...

 

HDU -- 4911 -- merge sort | tree Array

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.