Old Sorting(转化成单调序列的最小次数,置换群思想)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to sort the array in ascending order. A swap means, you can exchange any two elements of the array.
For example, let n = 4, and the array be 4 2 3 1, then you can sort it in ascending order in just 1 swaps (by swapping 4 and 1).
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains two lines, the first line contains an integer n (1 ≤ n ≤ 100). The next line contains nintegers separated by spaces. You may assume that the array will always contain a permutation of 1 to n.
Output
For each case, print the case number and the minimum number of swaps required to sort the array in ascending order.
Sample Input
3
4
4 2 3 1
4
4 3 2 1
4
1 2 3 4
Sample Output
Case 1: 1
Case 2: 2
Case 3: 0
题解:求转化成单调序列的最小次数;蓝桥杯那题一样。。。当初竟然没写出来。。。
有置换群的思想,对于每一个循环,只需要交换num - 1次就好了;把所有的加上就好了;
代码:
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- #define mem(x, y) memset(x, y, sizeof(x))
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int MAXN = ;
- int vis[MAXN];
- struct Node{
- int pos,v;
- friend bool operator < (Node a, Node b){
- if(a.v != b.v){
- return a.v < b.v;
- }
- else return a.pos < b.pos;
- }
- };
- Node dt[MAXN];
- int main(){
- int N, kase = , T;
- scanf("%d", &T);
- while(T--){
- scanf("%d", &N);
- for(int i = ; i <= N; i++){
- scanf("%d", &dt[i].v);
- dt[i].pos = i;
- }
- sort(dt + , dt + N + );
- mem(vis, );
- int ans = ;
- for(int i = ; i <= N; i++){
- if(!vis[i]){
- int num = ;
- int j = i;
- while(!vis[j]){
- vis[j] = ;
- num++;
- j = dt[j].pos;
- }
- ans += num - ;
- }
- }
- printf("Case %d: %d\n", ++kase, ans);
- }
- return ;
- }
Old Sorting(转化成单调序列的最小次数,置换群思想)的更多相关文章
- 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend
//把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...
- CF713C Sonya and Problem Wihtout a Legend & hihocoder1942 单调序列
这两个题是一样的,不过数据范围不同. 思路1: 在CF713C中,首先考虑使生成序列单调不下降的情况如何求解.因为单调上升的情况可以通过预处理将a[i]减去i转化成单调不下降的情况. 首先,生成的序列 ...
- 把一个序列转换成非严格递增序列的最小花费 POJ 3666
//把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...
- POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)
题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x ...
- 51nod1476 括号序列的最小代价
这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄÌ°ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...
- S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的
S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...
- 如何用python将一个时间序列转化成有监督学习
机器学习可以被用于时间序列预测. 在机器学习能使用之前,时间序列预测需要被重新转化成有监督学习.将一个序列组合成成对的输入输出序列. 在这篇教程中,你会发现如何通过使用机器学习算法将单变量和多变量的时 ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)
#include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...
随机推荐
- WPF动画
System.Windows.Media.Animation 这个命名空间中,包含了三种动画类:线性插值动画类(17个).关键帧动画(22个).路径动画(3个). 线性插值动画类(17个)如下: By ...
- Eclipse中使用正则屏蔽Logcat中的某些Tag
在使用Eclipse进行Android真机调试的时候经常会出现满屏幕的LogCat,即使设定了根据程序分类也不行 经常会有 Dalvikvm InputMethod这样的Tag出现 给自己的应用设定T ...
- [ReactJS] DOM Event Listeners in a React Component
React doesn't provide the listener to listen the DOM event. But we can do it in React life cycle: So ...
- boost 无锁队列
一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...
- jquery滚动到指定元素,模仿锚点
html <div class="pd-nav"> <div class="n-item active"> 保险服务 <i> ...
- 数据结构——左高树
一.扩充二叉树 考察一棵二叉树,它有一类特殊的节点叫做外部节点( external node),用来代替树中的空子树,其余节点叫做内部节点( internal node).增加了外部节点的二叉树被称为 ...
- PHP学习笔记二十一【全局变量】
<?PHP //定义全局变量 global $a; $a=9; //给全局变量赋值 function test1() { global $a; $a=45; } test1(); echo $a ...
- Java IO4:字符流进阶及BufferedWriter、BufferedReader
字符流和字节流的区别 拿一下上一篇文章的例子: 1 public static void main(String[] args) throws Exception 2 { 3 File file = ...
- (原+转)ubuntu16中安装opencv2.4.11
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638117.html 参考网址: http://www.cnblogs.com/jeakon/arch ...
- 自动运行native2ascii 命令的Bat文件的编写
使用eclipse开发,对于.properties文件的国际化,如果不使用插件对文件进行转码,则需要使用native2ascii命令自行对文件进行转码. 为了更方面的执行此操作,我将该 ...