Issue A: Maximum sub-sequence and problem time limit: 1 Sec Memory limit: 4 MB
Submitted by: 184 Resolution: 66
Submit State Algorithm FAQ Title Description
Given an integer sequence a1, A2, ..., an, a1~an a subsequence Ai~aj, making it from Ai to AJ and Max.
You only need to ask for the maximum number of sub-sequences, without requiring the largest sequence.
Enter a set of integers separated by a space between numbers and numbers. Outputs the maximum number of sub-sequences in the integer sequence and
Sample input-2 11-4 13-5-2 |
Sample output20 |
It is suggested that the algorithm of O (N3) can be obtained by using the exhaustive method, and the algorithm of O (N2) will be optimized. The two algorithms will time out.
The idea of using divide and conquer can have an O (N*LOGN) algorithm.
There is also a clever algorithm, its complexity is O (n).
Also note: The number of inputs is unknown. Please think about how to handle it. Here's the answer, Http://www.cnblogs.com/likaiming/p/8570205.html. Question B: All reverse-order time limit: 2 Sec Memory limit: 5 MB
Submitted by: 289 Resolution: 49
Submit State Algorithm FAQ Title Description
Give a string array, if (by dictionary order) a large string in front of a string smaller than it we call these two strings to form an "inverse pair". You need to find all the number of reverse pairs.
Input
The first line is the array size, and the second row is a space-delimited array of strings.
Note: You know in advance that each string has a length of 10.
Output
The number of all reverse pairs. This time we need to output a string, which is the "I have read the description of plagiarism" in Hanyu Pinyin. Output this line of submission we will think that we have fully read and understood the " description of plagiarism " announcement .
Note: The result is relatively large, please save it with a long type.
Sample input3AAAAAAAAAA CCCCCCCCCC BBBBBBBBBB |
Sample outputWo yi Yue du guan Yu Chao xi de Shuo ming1 |
Tips
Do not use brute force algorithms
Answer
The brute force solution will time out
Long LongA (vector<string>& V,intPOS1,intPos2) { Long LongW =0; for(inti = POS1; I <= Pos2; ++i) { for(intj = i +1; J <= Pos2; ++j) {if(V[i] >V[j])++W; } } returnW;}
BF
The idea of merging sorts is simply to add a statistic, and a line of code, on top of the merge sort, but the following practices exceed the memory limit
voidMerge (vector<string>& V,intPOS1,intMidintPos2,Long Long&nums) { //array starting from 0, if two positions are P1 and P2 respectively, where p1<p2, then p1 to P2 distance element has p2-p1+1vector<string> n1 (V.begin () +pos1,v.begin () +mid+1); Vector<string> n2 (V.begin () +mid+1, V.begin () +pos2+1); N1.emplace_back ("zzzzzzzzzzzzzzzzzzzz"); N2.emplace_back ("zzzzzzzzzzzzzzzzzzzz"); intN1_pos =0; intN2_pos =0; for(inti = POS1; I <= pos2;++i) {if(N1[n1_pos] <=N2[n2_pos]) {V[i]=N1[n1_pos]; ++N1_pos; }Else{ //Add the statistics here.Nums + = N1.size ()-n1_pos-1; V[i]=N2[n2_pos]; ++N2_pos; } }}voidMergeSort (vector<string>& V,intPOS1,intPos2,Long Long&nums) { if(Pos1 <Pos2) { intMid = (pos1 + pos2)/2; MergeSort (v,pos1,mid,nums); MergeSort (V,mid+1, pos2,nums); Merge (V,pos1,mid,pos2,nums); }}//This is a test caseintMain () {intNums =0; CIN>>Nums; stringtemp; Vector<string> V (nums,""); for(inti =0; i < nums; ++i) {cin>>temp; V[i]=temp; } Vector<string> v = {"aaaaaaaaaa","CCCCCCCCCC","bbbbbbbbbb" }; Long Longresult =0; MergeSort (V,0, V.size ()-1, result); cout<<"wo yi Yue du guan Yu Chao xi de Shuo Ming"<<Endl; cout<< result <<Endl; //System ("pause"); return 0;}
Merge Solution
The big guy reminds me not to assign vectors at recursion, but to apply a help vector before we start recursion, so that we can reduce memory usage, but the method of using sentinel above is useless, the code is slightly more complicated, but it is still a merge sort plus a line of statistical code.
voidMerge (vector<string>& V,intPOS1,intMidintPos2,Long Long& Nums, vector<string>&Help ) { //array starting from 0, if two positions are P1 and P2 respectively, where p1<p2, then p1 to P2 distance element has p2-p1+1 for(inti = POS1; I <= Pos2; ++i) help[i]=V[i]; intN1_pos =pos1; intN2_pos = mid+1; for(inti = POS1; I <= pos2;++i) {//When an array has traversed to completion if(N1_pos = = Mid +1) { while(N2_pos! = Pos2 +1) {V[i+ +] = help[n2_pos++]; } Break; } if(N2_pos = = Pos2 +1) { while(N1_pos! = mid +1) {V[i+ +] = help[n1_pos++]; } Break; } //Other conditions if(Help[n1_pos] <=Help[n2_pos]) {V[i]=Help[n1_pos]; ++N1_pos; }Else{ //Add the statistics here.Nums + = mid-n1_pos+1; V[i]=Help[n2_pos]; ++N2_pos; } }}voidMergeSort (vector<string>& V,intPOS1,intPos2,Long Long& nums,vector<string>&Help ) { if(Pos1 <Pos2) { intMid = (pos1 + pos2)/2; MergeSort (V,POS1,MID,NUMS,HELP); MergeSort (V,mid+1, Pos2,nums,help); Merge (V,POS1,MID,POS2,NUMS,HELP); }}//This is a test caseintMain () {intNums =0; CIN>>Nums; stringtemp; Vector<string> V (nums,""); for(inti =0; i < nums; ++i) {cin>>temp; V[i]=temp; } //vector<string> v = {"4", "3", "2", "1"};vector<string> Help (V.size ()," "); Long Longresult =0; MergeSort (V,0, V.size ()-1, Result,help); cout<<"wo yi Yue du guan Yu Chao xi de Shuo Ming"<<Endl; cout<< result <<Endl; System ("Pause"); return 0;}
merge sort memory optimization
Nan da algorithm design and Analysis course OJ Answer (2)