题目:

A narrow gauge train drives the visitors through the sequence of chambers in the Dark Ride attraction. The chambers are occupied by IT monsters which are specially programmed to scare the visitors in various wicked ways. There is one monster in each chamber. For strange and obscure reasons, some of the monsters might have been installed in wrong chambers. The task of Freddie and Morcia, who themselves are employees and not monsters, is to reinstall the monsters in the correct chambers.

To avoid any additional confusion or threat, Freddie and Morcia work in episodes. In one episode, they choose two distinct chambers. Freddie picks the monster in one of the chambers and transports it to other chamber, while Morcia picks the monster in the other chamber and transports it to the chamber from which Freddie has just picked his monster. Thus, Freddie and Morcia effectively swap the monsters in two chambers during one episode. After some number of episodes, all monsters should be placed in correct chambers. Swapping monsters is a tedious task. Quite understandably, Freddie and Morcia want to minimize the number of episodes.

Input Specification:

There are more test cases. Each test case consists of two lines. The first line contains one integer N (1 ≤ N ≤ 2 · 105 ) specifying the number of chambers. The chambers are labeled 1, 2, . . . , N and also the monsters are labeled 1, 2, . . . , N. The label of each chamber should coincide with the label of a monster correctly installed in it. The second line contains N labels of the monsters which are currently installed in the chambers. The monster denoted by the first label on the line is installed in the chamber 1, the monster denoted by the second label on the line is installed in the chamber 2, etc.

Output Specification:

For each test case, print a single line with one integer denoting the minimum number of episodes required to install the monsters in correct chambers.

题意:

求给出的一串序列,最少经过多少步可以,转换为严格的升序。转换的规则是每次只能交换两个数的位置。

思路:

每次交换都要至少确定一个数的位置,这样遍历一遍给出的序列就ok了。其实某些情况下,越往后交换的次数是越少的,因为前边的交换可能确定了两个数的位置。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5+;
int pos[maxn],m[maxn]; int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i = ; i<=n; i++){
scanf("%d",&m[i]);
pos[m[i]] = i;//存一下当前这个数的位置
}
int step = ;
for(int i = ; i<=n; i++){
if(m[i] != i){
int lt = m[i];//存一下交换之前处于i这里的数字
step++;
swap(m[i],m[pos[i]]);
pos[lt] = pos[i];//更新一下交换之后的数字的位置
}
}
printf("%d\n",step);
}
return ;
}
/*
PutIn:
3
1 2 3
5
5 4 3 2 1
5
5 4 1 2 3
4
4 3 1 2
6
5 6 3 4 1 2
10
9 8 10 5 3 2 4 7 6 1
PutOut:
0
2
3
3
2
9
*/

Gym - 101670H Dark Ride with Monsters(CTU Open Contest 2017 贪心)的更多相关文章

  1. Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

    题目: To encourage visitors active movement among the attractions, a circular path with ice cream stan ...

  2. Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)

    题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...

  3. Gym - 101670H Go Northwest!(CTU Open Contest 2017 思维题+map)

    题目: Go Northwest! is a game usually played in the park main hall when occasional rainy weather disco ...

  4. Gym - 101670A Amusement Anticipation(CTU Open Contest 2017 签到题)

    题目&题意: 倒着找处于最后位置的等差数列的开头的位置. 例: 1 5 3 4 5 6 3 4 5 6是等差数列,它的开头的位置是3 PS: 读题真的很重要!!!!多组输入,上来就读错了!! ...

  5. Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)

    题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: ...

  6. Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)

    题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...

  7. Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)

    题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下)  Bishop(斜 ...

  8. Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)

    题目: The park management finally decided to install some popular boxing machines at various strategic ...

  9. CTU Open Contest 2017

    这场题很水.水题我就懒得贴了. B - Pond Cascade 优先队列维护这个水池需要多少时间 或者 直接扫一遍. #include <cstdio> #include <cst ...

随机推荐

  1. 查看jvm常用命令

    jinfo:可以输出并修改运行时的java 进程的opts. jps:与unix上的ps类似,用来显示本地的java进程,可以查看本地运行着几个java程序,并显示他们的进程号. jstat:一个极强 ...

  2. 彻底弄懂px,em和rem的区别

    国内的设计大师都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? px特点: 1.IE无法调整那些使用px作为单位的字体大小: 2.国外大部分网站能够调整的原因 ...

  3. 2018.09.09 DL24 Day2总结

    今天挂的有点惨…… T1.forging 这道题自己在考试的时候想出来了…… 这题是一个期望递推.我们首先考虑这么一件事,一枚硬币,你抛到正面停止,抛到反面继续抛,问期望抛的次数.是两次.我们假设期望 ...

  4. python-----利用filecmp删除重复文件

    以下代码素材自取:链接:https://pan.baidu.com/s/1fL17RjKyGjpvpeeUFONCaQ  提取码:zgiw # coding:utf-8 import os impor ...

  5. 移动前端第一弹:viewport详解

    前言 这次想聊聊移动开发相关的事.是的,你没有看错,一句话就可以开始你的移动前端开发. 你心里一定在想,什么话这么酷,能够瞬间带入到移动前端开发的世界. 但其实它一点也不新奇,不复杂. viewpor ...

  6. 通过usb访问mtp设备(ubuntu12.04) (转载)

    转自:http://robert.penz.name/658/howto-access-mtp-devices-via-usb-on-ubuntu-12-04/ A friend asked me h ...

  7. bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】

    先考虑只能往一边传播,最后正反两边就行 一向右传播为例,一头牛能听到的嚎叫是他左边的牛中与高度严格小于他并且和他之间没有更高的牛,用单调递减的栈维护即可 #include<iostream> ...

  8. E - Cheap Kangaroo(求多个数的最大公约数)

    Description There are N kangaroos going out to eat at an Indian restaurant. The ith kangaroo wants t ...

  9. 10步成为专业iOS开发者——新手向,从零起步

    想在移动开发大餐中分一杯羹,体验最火最炫的技术?小伙子,很有眼光嘛 毫无疑问,移动开发在目前和未来几年内都会盛极一时.无数开发职位虚位以待.各大公司都在寻找各种层次的程序员——新手级.入门级.中级.老 ...

  10. EasyUI tree 异步树与采用扁平化实现的同步树

    所谓好记性不如烂笔头,为了以防忘记,才写下这篇博客,废话不多.. 异步树: tips:   可以采用easyui里的原始数据格式,也可以采用扁平化的数据格式. 使用场景: 当菜单模块数量庞大或者无限极 ...