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. android xml 布局错误(黑掉了)Missing styles. Is the correct theme chosen for this layout?

    发现在调整页面的时候 ,老是报以下错误,导致无法静态显示ui效果. Missing styles. Is the correct theme chosen for this layout? Use t ...

  2. IIS中查看W3P.exe进程对应的应用程序池的方法

    对于IIS6可以运行 iisapp -a来显示应用程序池的性能列表. iisapp.vbs它是一个脚本程序,在安装2003时需要SP1才有 而IIS7可以直接用它的外壳命令 C:"Windo ...

  3. 完全变味的Windows Azure Marketplace中国版

    国际版的Microsoft Azure很早就发布Marketplace了,里面有非常丰富的(超过3000款)第三方应用.服务和虚拟机镜像可以购买.其定价模式也非常灵活,支持按需付费(pay as yo ...

  4. C++/C#互调步骤

    一.C#调用C++ dll步骤(只能导出方法):  * 1. c++建立空项目->源文件文件夹中添加cpp文件和函数  * 2. c++属性设置中,配置类型设置为动态库dll,公共语言运行时支持 ...

  5. Java Web开发之分页(ajax)

    1.需要用到的jar包.js文件 JSONArray().fromObject()需要的jar包: (1)commons-beanutils-1.8.3.jar (2)commons-collecti ...

  6. 《MySchool数据库设计优化》内部测试

    1) 在SQL Server 中,为数据库表建立索引能够( C ). A. 防止非法的删除操作 B. 防止非法的插入操作 C. 提高查询性能 D. 节约数据库的磁盘空间 解析:索引的作用是通过使用索引 ...

  7. ACCP7.0优化Myschool内侧题

    1) 在SQL Server 中,为数据库表建立索引能够(C ). 索引:是SQL SERVER编排数据的内部方法,是检索表中数据的直接通道 建立索引的作用:大大提高了数据库的检索速度,改善数据库性能 ...

  8. NODE.JS开发指南学习笔记

    1.Node.js是什么 Node.js是一个让JS运行在服务器端的开发平台,它可以作为服务器向用户提供服务.Node.js中的javascript只是Core javascript,或者说是ECMA ...

  9. [No000049]狗日的中年——姜文

    文件名 大小 [No000049]狗日的中年——姜文.7z 228KB

  10. ADB server didn't ACK 问题解决

    在命令行中运行adb shell 出现如下错误提示 C:\Documents and Settings\Administrator>adb shelladb server is out of d ...