zoukankan      html  css  js  c++  java
  • UVA 1611 Crane(起重机)(贪心)

    题意:输入一个1~n(1<=n<=10000)的排列,用不超过9^6次操作把它变成升序。每次操作都可以选一个长度为偶数的连续区间,交换前一半和后一半。

    提示:2n次操作就足够了。

    分析:从左到右依次将数字i放在位置i。

    设要将数字i放在位置i,而数字i现在在位置pos。

    (1)若(pos - i) * 2 + i - 1 <= n,则可以直接将i放在位置i。pos-i为连续区间的一半长度,i-1为i之前已经排好的数字个数。交换连续区间(i, i + (pos - i) * 2 - 1)

    如:1 3 4 2 5,将2放在位置2,现在1已经排好,只需交换连续区间(2,5),即将前一半3 4和后一半2 5交换,就可直接完成i放在位置i的操作。

    (2)若不满足(1),则暂时将在pos位置的数字i向前交换,直至满足条件(1)。

    操作为:若pos-i为奇数,则交换连续区间(i,pos);若pos-i为奇数,则交换连续区间(i+1,pos)。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    const int MAXN = 1e4 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN];
    vector<pair<int, int> > ans;
    void exchange(int l, int r){
        for(int i = l, j = l + (r - l + 1) / 2; j <= r; ++i, ++j){
            swap(a[i], a[j]);
        }
    }
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            ans.clear();
            int n;
            scanf("%d", &n);
            for(int i = 1; i <= n; ++i){
                scanf("%d", &a[i]);
            }
            for(int i = 1; i <= n; ++i){
                int pos = 0;
                for(int j = i; j <= n; ++j){
                    if(a[j] == i){
                        pos = j;
                        break;
                    }
                }
                if(pos == i) continue;
                if((pos - i) * 2 + i - 1 <= n){
                    ans.push_back(pair<int, int>(i, i + (pos - i) * 2 - 1));
                    exchange(i, i + (pos - i) * 2 - 1);
                }
                else{
                    if((pos - i) & 1){
                        ans.push_back(pair<int, int>(i, pos));
                        exchange(i, pos);
                    }
                    else{
                        ans.push_back(pair<int, int>(i + 1, pos));
                        exchange(i + 1, pos);
                    }
                    --i;
                }
            }
            int len = ans.size();
            printf("%d\n", len);
            for(int i = 0; i < len; ++i){
                printf("%d %d\n", ans[i].first, ans[i].second);
            }
        }
        return 0;
    }
    

     

  • 相关阅读:
    [Wap] 制作自定义WmlListAdapter来实现Mobile.List控件的各种效果
    [EntLib]UAB(Updater Application Block)下载
    jarhoo是一个很棒的地方
    [GoogleMap]利用GoogleMap地图的这个应用真是太狠了[1]
    [J2ME] VideoCoolala(MobileWebCam)开源说明
    [p2p]手机是否可以通过JXTA网络与PC机/PocketPC/WindowsMobile实现P2P呢?
    Android Layout XML属性
    什么是9.png
    android主流UI布局
    Android开发之旅: Intents和Intent Filters(理论部分)
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6378746.html
Copyright © 2011-2022 走看看