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 ...
随机推荐
- STS 新建mvc工程--helloworld
File--New--Spring Template Project 选择Spring MVC Project 填写项目名称和基础的包名 Finish之后就完成了. 把项目添加到服务器,然后运行.在浏 ...
- c#调用Excel绘制图表
c#调用Excel需要引用命名空间 using Microsoft.Office.Interop.Excel; 由于该程序不复杂,主要是根据不同数据画表和图,画的图像也并不复杂,因为画图和画表的操作会 ...
- Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6633311 在上一篇文章中,我 们分析了And ...
- qt 操作excel表格
自己编写的一个Qt C++类,用于操作excel表格,在Qt中操作excel需在.pro中增加CONFIG+=qaxcontainer配置. 1.打开Excel:objExcel = new QAx ...
- 谷歌推出情境感知API
在 Google I/O 2016 大会上,我们宣布推出新的 Google Awareness API,让您的应用可以利用快照和围栏智能应对用户情境,并且仅需占用极少量的系统资源. 所有开发者均可以通 ...
- 适用于CSS2的各种运动的javascript运动框架
<script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...
- 初学jquery遇见的两个小问题!
<body> <div id="divtest">div的内容</div> <div id="default&quo ...
- NSString / NSMutableString 字符串处理,常用代码 (实例)
http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...
- 简单的拖动手势控制侧拉view显示
通过 UIPanGestureRecognizer 手势来控制侧拉view的显示 在QHLViewController.m文件中,先添加一些宏定义和参数等等. #define QHLAnimatin ...
- String 字符串相等比较