Worfzyq likes Permutation problems.Caoshen and Mengjuju are expert at these problems . They have n cards,and all of the numbers vi on these cards are different . Because Caoshen doesn't

like disordered permutations,he wants to change the permutation into non-descending

permutation.He defines the operations:every time you can choose two digits casually,and

exchange the positions of them.Caoshen is lazy,he wants to know at least how many operations he

needs to change the permutation into non-descending one?

输入

There are multiple test cases. Each case contains a positive integer n,incicate the number of cards(n<=1000000) . Followed by n positive numbers integers v1,v2,...,vn (1≤vi≤n)

---the value of each card.

输出

Print the minmum operations in a signal line for each test case.

样例输入

51 3 2 5 4

样例输出

2
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn = 1e6 + 7;
struct Node{
	int v, i;
}a[maxn];
int size;
int cmp(const void *m, const void *n){
	return ((Node*)m)->v - ((Node*)n)->v;
}
bool vis[maxn];
int dfs(int x){
	vis[x] = true;
	if (vis[a[x].i])return 0;
	else return 1 + dfs(a[x].i);
}
int main(){
	freopen("in.txt", "r", stdin);
	while (~scanf("%d", &size)){
		for (int i = 0; i < size; i++)scanf("%d",& a[i].v), a[i].i = i;
		qsort(a, size, sizeof(Node), cmp);
		memset(vis, 0, sizeof(vis));
		long long int ans = 0;
		for (int i = 0; i < size; i++){
			if (!vis[i])ans += dfs(i);
		}
		printf("%lld\n", ans);
	}
	return 0;
}

东大oj-1511: Caoshen like math的更多相关文章

  1. 九度OJ 1511 从尾到头打印链表

    题目地址:http://ac.jobdu.com/problem.php?pid=1511 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例. 每一组测 ...

  2. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...

  3. UPC 2959: Caoshen like math 这就是个水题

    http://acm.upc.edu.cn/problem.php?id=2959 这就是个水题,之所以要写这个题是感觉很有纪念意义 用力看就是盲……23333333333333333 这个题就是最小 ...

  4. 东大OJ-Max Area

    1034: Max Area 时间限制: 1 Sec  内存限制: 128 MB 提交: 40  解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...

  5. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  6. 2015年NEUACM一月月赛题解

    A Money , money 时间限制: 1 Sec  内存限制: 128 MB 提交: 15  解决: 14 题目描述 Small K seen recently stock market rea ...

  7. 九度oj题目1511:从尾到头打印链表

    题目1511:从尾到头打印链表 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:6010 解决:1805 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包 ...

  8. 【剑指Offer面试编程题】题目1511:从尾到头打印链表--九度OJ

    题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例. 每一组测试案例包含多行,每行一个大于0的整数,代表一个链表的节点.第一行是链表第一个节点的值,依次类 ...

  9. 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力

    分析:a^b+2(a&b)=a+b  so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...

随机推荐

  1. mysql 错误 1221 Incorrect usage of union and order by

    今天有个项目出现了问题 问题原因是union和order by 的问题.关于这个问题的解决方案可以猛击下面的链接. http://blog.csdn.net/gtuu0123/article/deta ...

  2. kernel启动console_init之前console不可用时发生crash的调试方法

    http://code.google.com/p/innosoc/wiki/KernelBootCrashDebug 注: 如在i386_start_kernel中加入:early_printk(&q ...

  3. 景瑞地产商业智能BI整体实施过程

    1.1行业背景 1.1.1景瑞地产 景瑞地产成立于1993年,专注于房地产开发,并一直秉持“永远诚信.恪守专业.锐意进取.共赢未来”的核心价值观和“舒适之道”的企业使命. 景瑞地产,源自上海.通过多年 ...

  4. Wish You to Remember

    Just to myself: it is not complicate. And I don't know its internal principle now. (ms08-067) But I ...

  5. Markdown learning

    For details, please refer to Markdown # Markdown Learning #h1 ##h2 ###h3 ####h4 **Bond** *italic* Th ...

  6. cuda中thread id

    //////////////////////////////////////////////////////////////////////////// // // Copyright 1993-20 ...

  7. UART to Serial Terminal(转载)

    前一篇<UART Explained>介绍了UART的基本信息,重点分析了UART的信号.本文摘录的文章则重点介绍了波特率(Baud Rate)相关的内容,波特率越高,传输速度越快,但实际 ...

  8. BZOJ2429[HAOI2006]聪明的猴子[最小生成树 kruskal]

    2429: [HAOI2006]聪明的猴子 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 896  Solved: 575[Submit][Statu ...

  9. Java中的ReentrantLock和synchronized两种锁定机制的对比

    问题:多个访问线程将需要写入到文件中的数据先保存到一个队列里面,然后由专门的 写出线程负责从队列中取出数据并写入到文件中. http://blog.csdn.net/top_code/article/ ...

  10. Visio连接数据表实体外键[快捷记录]

    打开数据库模型图. 单击“常用”工具栏上的“连接线”工具. 将“连接线”工具放在父表的中心上,使表的四周出现轮廓线,然后拖到子表的中心.当子表出现轮廓线时,松开鼠标按钮. 两个连接点均变为红色,同时父 ...