LightOJ 1166 Old Sorting 置换群 或 贪心 水题

LINK

题意:给出1~n数字的排列,求变为递增有序的最小交换次数

思路:水题。数据给的很小怎么搞都可以。由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法。

/** @Date    : 2017-07-20 14:45:30
  * @FileName: LightOJ 1166 贪心 或 置换群 水题.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

int vis[N];
int a[N];
int q[N];
int main()
{
	int T;
	cin >> T;
	int c = 0;
	while(T--)
	{
		int n;
		scanf("%d", &n);
		MMF(vis);
		for(int i = 1; i <= n; i++)
		{
			scanf("%d", a + i);
			q[a[i]] = i;
		}
		int ans = 0;
		for(int i = 1; i <= n; i++)
		{
			vis[a[i]] = 1;
			int np = q[a[i]];
			int cnt = 1;
			while(q[np] != np && !vis[np])
				cnt++, vis[np] = 1, np = q[np];
			ans += cnt - 1;
		}
		printf("Case %d: %d\n", ++c, ans);

	}
    return 0;
}
时间: 2024-10-21 12:17:23

LightOJ 1166 Old Sorting 置换群 或 贪心 水题的相关文章

HDU1050(贪心水题)

Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan t

hdu 5835 Danganronpa 贪心+水题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5835 [题意]有n种礼物,每个有ai个,现在开始给每个人发礼物,每人一个普通礼物和神秘礼物,相邻两人的普通礼物必须不同,每个礼物都可以作为神秘礼物/普通礼物,问最多可以发给多少人. [解题思路] 答案肯定是小于等于sum/2,因为每个小朋友得有2礼物.然而数据太水,sum/2也过了. 我们先考虑平凡的礼物,因为平凡的礼物相邻桌子上不能相同,故先把数量最多的礼物相邻的交替用作平凡的礼物. 如果n=3,

HDU 4334——Trouble——————【贪心&amp;水题】

Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5388    Accepted Submission(s): 1494 Problem Description Hassan is in trouble. His mathematics teacher has given him a very difficult pro

Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)

Many years ago Berland was a small country where only nn people lived. Each person had some savings: the ii -th one had aiai burles. The government considered a person as wealthy if he had at least xx burles. To increase the number of wealthy people

uva11389巴士司机问题(贪心水题)

 题目:有n个上午的任务和下午的任务,分配给司机,如果工作总时间超过d,超过的部分要给加班费: 现在让你安排任务,问最小的加班分花费. 思路:将下午的工作时间a按降序排序,晚上时间b按升序排序,不难发现只有将这两组按下标一一配对才能是总花费最小(将任意b[i],b[j]调换位置都不如原来的方案更优) #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include&

poj Yogurt factory (贪心水题)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int main() { int i,n,s; __int64 sum ; int cost[10010],num[10010]; while(~scanf("%d%d",&n,&s)) { for(i=0;i<n;i++) scanf("%d%d&qu

装箱问题 (贪心水题)

[题目描述] 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6.这些产品通常使用一个 6*6*h 的长方体包裹包装然后邮寄给客户.因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的包裹数量. [题目链接] http://noi.openjudge.cn/ch0406/19/ [算法] 由于需要装箱的产品总面积一定,则最优情况是箱子浪费面积最少.6,5,4,3所需包裹数是一定的,然后尽可能

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

HDU1009_FatMouse&amp;#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g