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;
}

最新文章

  1. 安装rabbitMQ delayed-messaged
  2. java list随机打乱
  3. 使用 PHP 和 Apache Solr 实现企业搜索
  4. TokuDB的特点验证
  5. JavaScript声音播放
  6. How to import the www.googleapis.com SSL CA certification to the jks store file?
  7. Bootstrap入门(二十九)JS插件6:弹出框
  8. Ibatis insert语句插入null引发的错误
  9. 第17章 社区快速入门和模板 - Identity Server 4 中文文档(v1.0.0)
  10. react初探(一)之JSX、状态(state)管理、条件渲染、事件处理
  11. ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5
  12. OPPO R6007在哪里打开usb调试模式的完美流程
  13. netty的解码器和粘包拆包
  14. [转] Understanding-LSTMs 理解LSTM
  15. BZOJ2595 WC2008游览计划(斯坦纳树)
  16. 深入出不来nodejs源码-内置模块引入再探
  17. 转:三种状态对象的使用及区别(Application,Session,Cookie)
  18. Thinkphp学习笔记5-URL生成U方法
  19. Detailed ASP.NET MVC Pipeline
  20. Entity Framework 数据生成选项DatabaseGenerated【转】

热门文章

  1. Android控件开发之Chronometer(转)
  2. OpenStack 企业私有云的若干需求(9): 云管理平台 CMP
  3. high三个晚上这样好么-JSON&amp;PHP
  4. .Net程序员之Python基础教程学习----列表和元组 [First Day]
  5. 关于电磁场中的E.B.D.H的理解
  6. CF719C. Efim and Strange Grade[DP]
  7. U3D的飞船太空射击例子中,使用coroutine
  8. Linux设备驱动之中断支持及中断分层
  9. java自带线程池和队列详细讲解
  10. http协议(五)web服务器