Foxesoftheroundtable Test Instructions:
Give you a sequence that allows you to generate an arrangement, and this arrangement is circular. So that the maximum value of the difference of any two neighbors is as small as possible.
Exercises
Order first, then insert from small to large. At this point, it is easy to find, to get the best strategy, only the current number is inserted next to the largest number, this is because the number to be inserted is greater than any number in the current arrangement, so the insertion will only produce a larger gap, in order to make the gap as small as possible, it has to be inserted next to the largest number.
If the current sequence is 0 1 2 3 4 5
First three numbers are not affected: 0 1 2
Insertion 3:0 1 3 2
Insertion 4:0 1 3 4 2
Insertion 5:0 1 3 5 4 2
According to the previous inference, we know that we are always inserting the current number in the middle of the entire sequence, because the number is in the middle, and it is the odd and even alternating. Thus, the replacement is made by the husband, then the original sequence is sorted, and then the result is returned.
Code:
#include <iostream>#include<vector>#include<cstring>#include<algorithm>#defineMax_n 55using namespacestd;intN;vector<int>Res;structnode{ Public: intVal,pos;};BOOLCMP (node A,node b) {returna.val<B.val;} Node X[max_n];classfoxesoftheroundtable { Public: Vector<int> Minimaldifference (vector<int>h) {n=h.size (); Res.push_back (0); if(n = =1)returnRes; Res.push_back (1); if(n = =2)returnRes; Res.push_back (2); if(n = =3)returnRes; Res.clear (); for(inti =0; I < n; i++) X[i].val = H[i], X[i].pos =i; Sort (x, x+N, CMP); Res.push_back (x[0].pos); for(inti =1; I < n; i + =2) Res.push_back (X[i].pos); for(inti = ((N-1) >>1) <<1; i >0; I-=2) Res.push_back (X[i].pos); returnRes; }};
View Code
Topcoder SRM 662 DIV1