首页 > 其他分享 >UVA6588 - Crane

UVA6588 - Crane

时间:2022-11-18 16:36:47浏览次数:62  
标签:cut int mid pos vis UVA6588 Crane include


​Regionals 2013​​​ >> ​​​ Europe - Central​

​6588 - Crane​

题意:给定一组无序数,从1-n,要求排序,排序的要求是选定一个区间[a,b],则这个区间的前一半和后一半互换位置,a到b有偶数个数,问交换几次。

解题思路:由于数字个数不多,可以枚举1-n个位置,枚举第i个位置,就找到第i个点所在点的位置,如果可以直接交换就交换,如果不能就一半一半交换。


#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <map>

using namespace std;

const int inf = 0x3f3f3f3f;
const int Max = 10100;
typedef long long int LL;
int a[Max],vis[Max], n, rr[531500], ll[531500];
int cut;
void exch(int l, int r)
{
int i, t;
int mid = r-l;
ll[cut] = l;
rr[cut++] = r+mid-1;
//printf("%d %d\n", l, r+mid-1);
for(i = l; i < r; i++)
{
t = a[i];
a[i] = a[i+mid];
a[i+mid] = t;
t = vis[a[i]];
vis[a[i]] = vis[a[i+mid]];
vis[a[i+mid]] = t;
}
/*for(i = 1; i <= n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;*/
}

int main()
{
int i, pos, j;
int K;
scanf("%d", &K);
while(K--)
{
scanf("%d",&n);
cut = 0;
for(i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
vis[a[i]] = i;
}
for(i = 1; i <= n; i++)
{
if(vis[i] == i)
continue;
pos = vis[i];
while(a[i] != i)
{
int end = i+(pos-i)*2-1;
if(end <= n)
{
exch(i, pos);
}
else
{
int mid = pos - i +1;
if(mid%2 == 0)
{
exch(i, i + mid/2);
pos = vis[i];
}
else
{
exch(i+1, i+mid/2+1);
pos = vis[i];
}
}
}
}
printf("%d\n", cut);
for(i = 0; i < cut; i++)
{
printf("%d %d\n", ll[i], rr[i]);
}
}
return 0;
}




标签:cut,int,mid,pos,vis,UVA6588,Crane,include
From: https://blog.51cto.com/u_15879559/5868808

相关文章

  • crane
    Craneorcranesmayreferto:Crane(bird),alarge,long-neckedbirdCrane(machine),industrialmachineryforliftingCrane(rail),acranesuitedforuseo......