zoukankan      html  css  js  c++  java
  • acdream 1038: nanae is a good girl 置换群循环因子

    解题思路:

      对于置换群     分解其循环因子   

      对于   需要对换一次,对于  需要对换两次, 所以当前置换群最少对换次数为三

      对于 本题, 我们先 求出其 循环因子后,再从小到大 进行贪心即可

    解题代码:

    View Code
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N = 100010;
    
    int a[N], b[N], n , k;
    bool vis[N];
    
    int check(int s){
        for( int i = s; i < n; i++)
            if( !vis[i] ) return i;
        return -1;
    }
    
    priority_queue< int,vector<int>, greater<int> > Q;
    int main()
    {
        while( scanf("%d%d", &n, &k ) != EOF )
        {
            while( !Q.empty() ) Q.pop();    
            memset( vis, 0, sizeof(vis));    
            memset( b, 0, sizeof(b));    
            int cnt = 0, x, pos;
            for(int i = 0; i < n; i++)
            {
                scanf("%d", &x); a[i] = x; 
                if( i == x ){ cnt++;vis[i] = true;} 
            }
            
            pos = 0;
            while(  (pos = check(pos) ) != -1 ){
                int size = 0, t = pos;
                while( !vis[t] )
                {    size++; vis[t] = true; t = a[t]; }
                Q.push( size );    
                pos++;
            }    
            while( !Q.empty() ){
                int t = (Q.top()) - 1; Q.pop();
    //            printf("t = %d\n", t+1 );    
                if( t > k ){ cnt += k; break; }
                else       { cnt += t+1; k -= t; }    
            }    
            printf("%d\n", cnt );
        }    
        return 0;
    }
  • 相关阅读:
    解决Maven关于本地jar包的打包处理
    微信公众帐号应用开发—本地调试
    字符串
    动态类型
    数字
    React文档(二十四)高阶组件
    React文档(二十三)Web Components
    React文档(二十二)context
    React文档(二十一)协调
    React文档(二十)不使用JSX
  • 原文地址:https://www.cnblogs.com/yefeng1627/p/2834395.html
Copyright © 2011-2022 走看看