UVa - 10570 - Meeting with Aliens


对于n个数字,调整成从大到小或者从小到大,操作数都不超过n,每一次操作至少有一个数字放到正确的位置。

枚举起点还有序列正反,求出最小的交换次数。

求交换次数的时候用贪心思想,先把1交换到1,再把2交换到2。。。

AC代码:

#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 = 505;
int a[1005], b[1005];
int n;

int solve() {
	int ret = 0xfffffff;
	for (int i = 0, j = n; i < n; i++, j++) {
		a[j] = a[i];
	}
	for (int i = 0; i < n; i++) {
		int mark[maxn], cnt = 0;
		for (int j = 0; j < n; j++) {
			mark[a[i + j]] = i + j;
			b[i + j] = a[i + j];
		}
		for (int j = 0; j < n; j++) {
			if (b[i + j] != j) {
				cnt++;
				int idx = mark[j];
				swap(b[i + j], b[idx]);
				mark[b[idx]] = idx;
			}
		}
		ret = min(ret, cnt);
	}
	return ret;
}

int main() 
{
	ios::sync_with_stdio(false);
	while (cin >> n && n) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			a[i]--; // 方便处理
		}
		int ret;
		ret = solve();
		reverse(a, a + n); // 反转再算一次求最小
		ret = min(ret, solve());
		cout << ret << endl;
	}
	return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值