Uva 1611 Crane

Source: Internet
Author: User

Thinking about it:

For a sequence of length n, the largest number is N, then a maximum of 2 steps is required if you want to put it back in the nth position.

Let's start with a simple example:

Suppose the sequence: 1 2 3 6 5 4, the current maximum number is 6, need and 4 swap position, then can be regarded as: * * * 6 * 4, because 6 and 4 have a number, and 6 before there is three number, then can be 6 after the portion, and 6 (including) before the equal length of the exchange, can be: 1 2   5 4 3 6, only one step is required. If, if, the number before 6 is less than the number between 6 and 4, such as 1 6 2 3 4 5? Then you can exchange the entire part before 4, you can get:2 3 1 6 4 5, there is now a step from the back.

You may ask, why the exchange of half will certainly be able to repeat the last step. In fact, this can be proved, assuming that the number before 6 is X, and the number before 6 and 4 has y, and X < Y, assuming that the exchange is half, then 6 of the number has (x + (x+y+1)/2), while the number between 6 and 4 is ((x+y+1)/2-x- 1), although there may be a little change due to the parity of the x+y+1, but the relative size has been determined, that is, after the transformation of 6 items will still be greater than or equal to 6 and 4 of the number of items

  

  The following can be done:

For a sequence with a length sequence of N, you can turn n,n-1,n-2, ..., 3, 2 back in place, while placing up to two steps each time, so a total of up to 2N steps.

Reference:

1. Algorithmic Competition Introduction Classic (2nd edition)

Ps:

Just began to use bubbling ideas, exchange of neighboring numbers, but need n^2 times exchange, inevitably inconsistent with the proposal. Later thought, in order to put the current largest number to the last, do not need to exchange the adjacent two number, you can try in one step operation, Exchange more numbers, so for the number to be exchanged, the distance of the move is large, but the operation is only counted in one step.

Code:

/** * AC @ Sep 16th * Run time:0.322s */#include <bits/stdc++.h>using namespace std;typedef std::p air<int, int> node;vector<int> crane_vec;vector< Node > Ans;int n;int Search (vector<int> &lst, int value {for (Vector<int>::iterator it = Lst.begin (); It! = Lst.end (); ++it) {if (*it = = value) {R        Eturn It-lst.begin (); }} return-1;}    void Read () {crane_vec.clear ();    Ans.clear ();    Ensure that the first element was counted from 1 ans.push_back (make_pair (0, 0));    Crane_vec.push_back (-1);    Cin >> N;    int tmp;        for (int i = 0; i < N; ++i) {cin >> tmp;    Crane_vec.push_back (TMP);    }}//swap from start to end as the problem tellsvoid swap (int start, int end) {Ans.push_back (Make_pair (Start, end));    int mid = (start + end) >> 1;        for (int i = start; I <= mid; ++i) {int t = Crane_vec[i]; Crane_vec[i] = crane_vec[mid + 1 + i-start];        Crane_vec[mid + 1 + i-start] = t;    }}void done () {int target;            while (n > 1) {if (Crane_vec[n]! = N) {target = search (Crane_vec, N); Fr:the useless element (s) before n//bk:the useless element (s) between N and the last element in crane            _vec int fr = target-1;            int bk = n-target-1; if (FR < BK) {//odd or even:the seap operation must need even elements swap (1, N &amp ; 1?                N-1: N-2);                target = Search (Crane_vec, N);                FR = target-1;            BK = N-1-target;        } swap (N-2 * bk-1, N);    } N--;    }}void out () {cout << ans.size ()-1<< Endl; For (vector< Node >::iterator it = ans.begin () + 1; it! = Ans.end (); ++it) {cout << it->first <& Lt    "<< It->second << Endl;    }}void work () {read ();    Done (); OUT ();}    int main () {Ios::sync_with_stdio (false);    Cin.tie (0);    int T;    Cin >> T;    while (T-) {work (); } return 0;}

  

Uva 1611 Crane

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.