Question: only the sorting of adjacent elements can be exchanged, and the minimum number of exchanges is counted.
Analysis: grouping, reverse order number. In the process of merging and sorting, the reverse order is solved.
When merging strings a and B, two pointers are used as the Count;
When elements in B are added to a new array, the remaining elements in a must be greater than B;
You can add each count.
Note: The replacement group can be used to calculate any minimum exchange... )
# Include
Link: poj 2299 ultra-quicksort
After finding the number of Adjacent Elements in a series, it is a series of ascending values.
Idea: Calculate the reverse number of the string sequence. The data is relatively large and needs to be discretization.
AC code:
# Include
The topic is to let you ask for reverse number, with a tree-like array is very simple, but the data is too large, to be discretized first, the data range compressed to 1~n within. and POJ doesn't support c++11, it's killing me. Lambda expression compilation error.#include Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source. poj2299 (Ultra-quicksort) tree-like array + discretization
Question link: http://poj.org/problem? Id = 2299
Ideas:
The number of reverse orders is the number of exchanges, so you can find the reverse Number of the sequence.
According to the divide and conquer method, the sequence is divided into two parts of the same size, respectively, to find the number of reverse orders of the sub-sequence; for each number in the right sub-sequence, find the number larger than its number in the left sequence, the sum of computation is the solution.
In addition, when
The main topic: Give a series, each exchange of adjacent numbers, to order the minimum number of times to increase the sequence.Analysis: Seeking reverse order number, merging sort#include #include #include #define MAXN 5000010using namespace Std;int a[maxn],tem[maxn],n;Long long Sort (int l,int R){if (l{Long Long L=sort (L, (l+r)/2);Long Long R=sort ((l+r)/2+1,r);int p=l,mid1= (L+R)/2,mid2= (l+r)/2+1;int k=l;while (p{if (mid2{Tem[k++]=a[mid2++];l+=mid1-p+1;}ElseTem[k++]=a[p++];}for (int i=l;iA[
http://poj.org/problem?id=2299Merge sort: For example now there is a sequence [l,r], we can divide this sequence into two sequences [L,mid], [mid,r], using recursion to follow theThe method gradually reduces the sequence, the order of the sub-sequence, and then the sequence of sub-sequences, and then the orderly interval of the merger, very good drop embodies the idea of division.Number of reverse order (if there is i#include #include#include#include#include#include#includeusing namespacestd;#de
PHPfunctionInsert_sort ($arr){ $count=Count($arr); for($i= 1;$i$count;$i++){ $tmp=$arr[$i]; $j=$i-1; while($arr[$j]>$tmp){ $arr[$j+1] =$arr[$j]; $arr[$j] =$tmp; $j--; if($j){ Break;//here with continue still error, do not know what yuan because? The error is that the array index appears negative } } } return $arr;}$arr=Array(49,38,65,97,76,13,27);Print_r(Insert_sort ($arr));InsertsortPHP quickSor
Private intPartitionint[] nums,intLointHi) { if(lo >=Hi) { returnlo; } inti =lo; intj = Hi + 1; intv =nums[lo]; while(true) { while(nums[++i] V)if(i = = Hi) break; while(nums[--j] >V)if(j = Lo) break; if(i >=J) { break; } intTMP =nums[i]; nums[i]=nums[j]; nums[j]=tmp; } nums[lo]=nums[j]; nums[j]=v; returnj; }Lo and hi are Inclusive. In the returned array, the number of index less than J is equal to the number of J,index greater than J is greater
element also conforms to the definition (all I to the left of the object is less than the Shard object because I will continue to look for the element to the right, all the elements I position now must be less than the Shard element, otherwise not conform to the definition I left the element is less than the Shard element) and (all J right object is greater than )So you can go down.Continue to find the element greater than 49 starting from the position i = 2, when i = 3 is found value = 97 Con
to help you give some (n) the number of disorderly order , let you ask the number of times a bubble sort needs to be exchanged (nObviously can't directly simulate the bubble sort, in fact the number of exchanges is the sequence of the reverse logarithm.Because the data range is 0≤a[i]≤999,999,999, we need to discretization first, and then use the appropriate data results to find out the reverse orderYou can add the a[i] step-by-step with a segment tree, and add as many as you like before adding
Merge sort the first time do, turn over the book to look at the merger of ideas to see someone else's blog.http://poj.org/problem?id=2299#include Code Implementation Reference: Http://www.slyar.com/blog/poj-2299-c.htmlProblem-Solving ideas reference: http://blog.csdn.net/lyy289065406/article/details/66473462299 ultra-quicksort (merge sort)
of n elements.For example: 100 people randomly play on the playground, the teacher said: Let's line up, from short to high, listen to my command. You first in a row, no matter how tall.OK, it's in a row. The teacher said: All the taller than the first one occupies the left column. It's all lined up then.Then the teacher said: "Now everyone has seen it." You are two columns now. Now, I'm going to say a standard, you follow the standard.That is: shorter than the first person in the column to ope
The number of adjacent values can be exchanged, and the number of consecutive values is not decreased after the number of times is exchanged .. if each number is smaller than the number on the left, it is exchanged with the number on the left .. a
/*
If this question is cyclically cracked, it will be TLE. There are two methods: 1. Merge and sort. 2. Tree array.
This question shows the Merge Sorting method: the main idea of splitting is to continue to be divided into two parts, and the two
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.