天天看點

Benelux Algorithm Programming Contest 2020 H Incomplete Implementation

H Incomplete Implementation

題意:

有一個算法,每次可以對數組的任意一半子序列進行排序,這個子序列可以是非連續的,如圖

Benelux Algorithm Programming Contest 2020 H Incomplete Implementation

每次排序之後再放回去,并且三次之内一定可以将序列排好,求每次排序的下标

思路:

每次把3n/4到n和後n/2到3n/4進行排序,最後把前1/2排序就好了,具體過程看代碼

代碼

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
const int N = 1e5+10;
int a[N], pos[N], b[N];
int n, cnt;
map<int, int> mp;
void solve(int l, int r)
{
    cnt = 0;
    for (int i = r; i > l; i--)
    {
        if (mp[i] <= l && mp[i] != 0) 
        {
            pos[++cnt] = mp[i];
            mp[i] = 0;
            b[cnt] = a[pos[cnt]];
        }
        pos[++cnt] = mp[a[i]];
        mp[a[i]] = 0;
        b[cnt] = a[pos[cnt]];
    }
    for (int i = 1; i <= n; i++)
    {
        if (cnt < n / 2 && mp[a[i]] != 0)
        {
            pos[++cnt] = mp[a[i]];
            mp[a[i]] = 0;
            b[cnt] = a[pos[cnt]];
        }
        if (cnt == n / 2)
        {
            break;
        }
    }
    sort(b + 1, b + 1 + n / 2);
    sort(pos + 1, pos + 1 + n / 2);
    for (int i = 1; i <= n / 2; i++)
    {
        a[pos[i]] = b[i];
        mp[b[i]] = pos[i];
        cout << pos[i] << " ";
    }
    cout << endl;
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        mp[a[i]] = i;
    }
    cout << 3 << endl;
    int k = 0;
    int l = n / 4 * 3;
    int r = n;
    solve(n / 4 * 3, n);
    solve(n / 2, n / 4 * 3);
    for (int i = 1; i <= n / 2; i++)
    {
        cout << i << " ";
    }
    return 0;
}

           

繼續閱讀