Old Sorting(转化成单调序列的最小次数,置换群思想)

Old Sorting

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1166

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 = 10010;
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 = 0, T;
    scanf("%d", &T);
    while(T--){
            scanf("%d", &N);
        for(int i = 1; i <= N; i++){
            scanf("%d", &dt[i].v);
            dt[i].pos = i;
        }
        sort(dt + 1, dt + N + 1);
        mem(vis, 0);
        int ans = 0;
        for(int i = 1; i <= N; i++){
            if(!vis[i]){
                int num = 0;
                int j = i;
                while(!vis[j]){
                    vis[j] = 1;
                    num++;
                    j = dt[j].pos;
                }
                ans += num - 1;
            }
        }
        printf("Case %d: %d\n", ++kase, ans);
    }
    return 0;
}
时间: 2024-07-29 03:52:35

Old Sorting(转化成单调序列的最小次数,置换群思想)的相关文章

把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend

1 //把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend 2 //dp[i][j]:把第i个数转成第j小的数,最小花费 3 //此题与poj 3666相似 a[i]转换成a[i]-i 4 5 #include <iostream> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <algorithm> 9 #include <

CF713C Sonya and Problem Wihtout a Legend &amp; hihocoder1942 单调序列

这两个题是一样的,不过数据范围不同. 思路1: 在CF713C中,首先考虑使生成序列单调不下降的情况如何求解.因为单调上升的情况可以通过预处理将a[i]减去i转化成单调不下降的情况. 首先,生成的序列中的每个数一定是原序列中的数.于是可以通过dp+离散化技巧解决.dp[i][j]表示把前i个数变成单调不下降的序列,并且a[i]不超过第j大的数所需要的最小代价. 实现1: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef lo

51nod 1476 括号序列的最小代价 (括号题套路+反悔贪心)

题意:给一串只有'(' , ')' , '?' 的括号序列,每个?可以变成)或者(,代价分别为bi和ai,求变成合法序列的最小代价 思路:学习自最近的网络赛&&51nod贪心专题视频的思想,“反悔”,一般在获取收益有限制的情况下使用 先按某种“优”的策略贪心,如果不满足限制条件了,取一个修改后代价尽可能小的状态修改成满足条件的状态,得到新的满足限制下的最优解 这种贪心常常可以借助优先队列实现 然后是括号题的套路:把(当做1,把)当做-1,做前缀和 这题中,先当做所有的?都换成右括号,这是显

把一个序列转换成非严格递增序列的最小花费 POJ 3666

1 //把一个序列转换成非严格递增序列的最小花费 POJ 3666 2 //dp[i][j]:把第i个数转成第j小的数,最小花费 3 4 #include <iostream> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <algorithm> 8 #include <vector> 9 #include <math.h> 10 // #include <memory.

HDU 4312 最小切比雪夫距离-转化成曼哈顿距离再分治

题意:二维空间,n个点,求以某点为起点到各点的最小切比雪夫距离 分析: 上一道题之前已经用"分治"思想在O(n)的时间内求出了n个点,以某点为起点到各点的最小曼哈顿距离,那么我们根据二维空间切比雪夫距离和曼哈顿距离的关系,可以把切比雪夫距离转化成曼哈顿距离,再直接用之前的方法即可. 二维空间: 曼哈顿距离 :d=|x1-x2|+|y1-y2|,到某点的曼哈顿距离为r的点组成一个边长为√2*r的正方形,且边与坐标轴成45度 切比雪夫距离:d=max(|x1-x2|,|y1-y2|),到某

1、AJAX里面status的值代表什么 2、get post 的区别 3、怎样把对象转化成字符串 4、闭包、继承、原型、原型链 5 、http传输协议 6、arguments是什么

1.AJAX里面status的值代表什么     在JavaScript里面写AJax的时,最关键的一步是对XMLHttpRequest对象建立监听,即使用"onreadystatechange"方法.监听的时候,要对XMLHttpRequest对象的请求状态进行判断,通常是判断readyState的值为4且status的值为200或者304时执行我们需要的操作.以下记录了一些常用readState以及status的值及其含义 readyState 属性表示Ajax请求的当前状态.它的

Opencv将图片转化成视频

// VideoWriter.cpp : 定义控制台应用程序的入口点. // //#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "iostream" using namespace std; int main(int argc, char* argv[]) { double fps = 29;//视频压缩帧率 CvSize size = cv

括号序列的最小代价

题意 查看原题 这里有一个关于合法的括号序列的问题. 如果插入"+"和"1"到一个括号序列,我们能得到一个正确的数学表达式,我们就认为这个括号序列是合法的.例如,序列"(())()", "()"和"(()(()))"是合法的,但是")(", "(()"和"(()))("是不合法的.我们这有一种仅由"(",")"

BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 368664-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SA