zoukankan      html  css  js  c++  java
  • UVA 10570 Meeting with Aliens(外星人聚会)(暴力枚举)

    题意:输入1~n的一个排列(3<=n<=500),每次可以交换两个整数。用最少的交换次数把排列变成1~n的一个环状序列。

    分析:正序反序皆可。枚举每一个起点,求最少交换次数,取最小值。

    求最小交换次数solve函数,将所有不需要交换的数字用cnt统计出来,而需要交换的数字集合(个数为n)交换次数是n-1,统计一次cnt。

    原因:如序列3 1 2 5 6 4,因为下标从0开始,因此将序列变为2 0 1 4 5 3,假设求以2为起点的序列正序最小交换次数,是先将2与1交换(即数字2换到位置2),再把1与0交换(即数字1换到位置1),这样交换的次数才最小。

    #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 = 500 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN << 1];
    int vis[MAXN];
    int solve(int a[], int n){//求最小交换次数
        memset(vis, 0, sizeof vis);
        int cnt = 0;
        for(int i = 0; i < n; ++i){
            if(!vis[i]){
                ++cnt;
                for(int j = i; !vis[j]; j = a[j]) vis[j] = 1;
            }
        }
        return n - cnt;
    }
    int main(){
        int n;
        while(scanf("%d", &n) == 1){
            if(!n) return 0;
            for(int i = 0; i < n; ++i){
                scanf("%d", &a[i]);
                --a[i];
                a[i + n] = a[i];
            }
            int ans = INT_INF;
            for(int j = 0; j < 2; ++j){//正序反序
                for(int i = 0; i < n; ++i){//枚举起点
                    ans = min(ans, solve(a + i, n));
                }
                reverse(a, a + 2 * n);
            }
            printf("%d\n", ans);
        }
        return 0;
    }
    

     

  • 相关阅读:
    C# 中的 ConfigurationManager类引用方法
    添加Word,Excel等dll时如何操作。
    Win7(64位)中IIS配置Access数据库的asp.net程序中出现“未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序”
    sql模糊查询
    Spring中AOP的使用
    MongoDB的孤儿文档是如何产生的
    Docker
    MySql索引优化
    Kafka(分布式流式系统)
    synchronized的底层实现
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6377841.html
Copyright © 2011-2022 走看看