东大oj-1511: Caoshen like math
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的更多相关文章
- 九度OJ 1511 从尾到头打印链表
题目地址:http://ac.jobdu.com/problem.php?pid=1511 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例. 每一组测 ...
- 东大OJ 2SAT 异或
看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...
- UPC 2959: Caoshen like math 这就是个水题
http://acm.upc.edu.cn/problem.php?id=2959 这就是个水题,之所以要写这个题是感觉很有纪念意义 用力看就是盲……23333333333333333 这个题就是最小 ...
- 东大OJ-Max Area
1034: Max Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 40 解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- 2015年NEUACM一月月赛题解
A Money , money 时间限制: 1 Sec 内存限制: 128 MB 提交: 15 解决: 14 题目描述 Small K seen recently stock market rea ...
- 九度oj题目1511:从尾到头打印链表
题目1511:从尾到头打印链表 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:6010 解决:1805 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包 ...
- 【剑指Offer面试编程题】题目1511:从尾到头打印链表--九度OJ
题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例. 每一组测试案例包含多行,每行一个大于0的整数,代表一个链表的节点.第一行是链表第一个节点的值,依次类 ...
- 玲珑学院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 ...
随机推荐
- 注解学习(模仿springMvc的注解注入方式)
最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着学习的态度来阅读源码,若文章在表述 ...
- Linux基础命令之cat使用方法大全
今天在学习部署安装openstack的时候,看到一个关于cat的奇怪用法,可能是本人的才疏学浅没见过这种写法,于是乎查阅资料了一番,并进行了总结,希望也能够帮助有需要的朋友. 以下是我总结的几种常用方 ...
- hdu1247 Hat’s Words
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...
- WinCE项目应用之RM905a+活度计远程检定方法研究
前文<RM905a+医用放射性核素活度计>中已经提到,基于WinCE5.0系统的RM905a+可以很方便的实现远程界面显示和控制.所以远程检定的主要工作在于服务器端的业务部分.基于< ...
- POJ3666Making the Grade[DP 离散化 LIS相关]
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6445 Accepted: 2994 ...
- AC日记——向量点积计算 openjudge 1.6 09
09:向量点积计算 总时间限制: 1000ms 内存限制: 65536kB 描述 在线性代数.计算几何中,向量点积是一种十分重要的运算. 给定两个n维向量a=(a1,a2,...,an)和b=(b ...
- Flex编译程序出现 Could not find compiled resource bundle 'SharedResources' for locale 'en_US'.
Flex编译程序出现 Could not find compiled resource bundle 'SharedResources' for locale 'en_US'. 而且静态类居然为nul ...
- git创建本地分支
git branch -b newbranch //创建并切换到newbranch分支下 git push origin newbranch //推送到远程仓库的newbranch分支下,没有就创建
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- Openjudge 1.13-40 提取数字串按数值排序
40:提取数字串按数值排序 查看 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个字符串,请将其中的所有数字串提取,并将每个数字串作为整数看待(假设可以用int 表示),按从 ...