II simulation game 9.11

The first question permutation

Eskimo is a reverse of the title (it does not describe the extent of bare)

If viewed from above, you will find that the role is the number of horizontal left and right sides to exchange it current. Thus the first part of this question may be down directly from the swap violence.

For the second portion, the upside down so that a map can be found in FIG reversed arrangement is put through a recovery order switching times m.

The least number of horizontal lines, which is the minimum switching frequency is the reverse of the number.

A first portion of a second portion of the analog exchange + merge sort in reverse order of the number of statistical points 100

#include<cstdio>
using namespace std;
const int N=1000007;
int n,m,hx[N],a[N],c[N],cnt;
void sort(int l,int r)
{
    if(l==r) return;
    int mid=l+r>>1;int t=l,q=mid+1,p=l-1;
    sort(l,mid);sort(mid+1,r);
    while(q<=r&&t<=mid)
    {
        if(a[t]>a[q])
        {
            cnt+=mid-t+1;
            c[++p]=a[q];q++;
        }
        else c[++p]=a[t++];
    }
    while(q<=r) c[++p]=a[q++];
    while(t<=mid)c[++p]=a[t++];
    for(int i=l;i<=r;i++) a[i]=c[i];
}
void swap(int &x,int &y)
{
    int temp=x;x=y;y=temp;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d",&hx[i]);
    for(int i=1;i<=n;i++) a[i]=i; 
    for(int i=1;i<=m;i++) swap(a[hx[i]],a[hx[i]+1]);
    for(int i=1;i<=n;i++) printf("%d ",a[i]);
    sort(1,n);
//-        for(int i=1;i<=n;i++) printf("%d ",a[i]);
    printf("\n%d",cnt);
}

When the examination is the road map that topic, I think in terms of that half thought out to give up. Still too young.

Guess you like

Origin www.cnblogs.com/IvyLH03/p/11517409.html