Topcoder SRM 662 DIV1

Source: Internet
Author: User

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

Related Article

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.