Uva-1611-crane

Source: Internet
Author: User


From left to right, put each number in place in turn. Put a number in the right place, observe the discovery of up to two steps
Assuming the operation to the first position, and I this number is exactly in the POS position, now it is necessary to determine whether the POS can be directly transferred to the position I
If i + (pos-i) * 2-1 <= N, the operation can be completed one time
In cases where the above conditions are not established, there are two different cases
One is that the distance between POS and I is odd: then the value of the interval [I,pos] is exchanged directly
The other is the case where the distance is even, then the value of this interval [I+1,pos] is exchanged

AC Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring > #include <string> #include <sstream> #include <vector> #include <set> #include <map># Include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert > #include <cmath> #include <functional>using namespace std;const int maxn = 10005;int numbers[maxn];void E Xchange (int L, int R) {for (int i = l, j = l + (r-l + 1)/2;j <= R; j + +, i++) {swap (numbers[i], numbers[j]);}} int main () {Ios::sync_with_stdio (false), int t;cin >> t;while (t--) {int n;cin >> n;for (int i = 1; I <= n; i + +) {cin >> numbers[i];} Vector<pair<int, int> > ans;for (int i = 1; I <= n; i++) {int pos;for (int j = i; J <= N; j + +) {if (numb ERS[J] = = i) {//Find position of I pos = J;break;}} if (pos = = i) {continue;} if (i + 2 * (pos-i)-1 <= N) {//satisfy this condition can be directly ans.push_back (Make_pair (i, i + 2* (Pos-i)-1); Exchange (I, i + 2 * (pos-i)-1);} else {if ((pos-i)% 2) {Ans.push_back (Make_pair (i, POS)); Exchange (I, POS);} else {ans.push_back (Make_pair (i + 1, POS)); Exchange (i + 1, POS);} i--;}} cout << ans.size () << endl;for (int i = 0; i < ans.size (); i++) {cout << ans[i].first << ' &L t;< ans[i].second << Endl;}} 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.