题目:

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. ios22--动画

    控制器: // // ViewController.m // 07-渐变动画 // // Created by xiaomage on 15/12/30. // Copyright © 2015年 小 ...

  2. ios20--xib2

    故事板控制器: // // ViewController.m // 03-通过xib自定义商品的View #import "ViewController.h" #import &q ...

  3. C# winfrom TCP 服务端和客户端(链接)

    1.C#Winform TCP 之服务端: 可以参考下面链接,比较好.第二个链接可以看看,提供了一个思路. http://www.cnblogs.com/guolebin7/archive/2013/ ...

  4. Java 反射 —— 运行时的类型信息

    1. 反射机制的由来 RTTI 机制可以告知某个对象的确切类型,但有一个前提,该类型在编译时必须已知(编译器在编译时打开和检查 .class 文件以获取类型信息).似乎是个很宽松的限制,但假如你获取了 ...

  5. 4.9 Parser Generators

    4.9 Parser Generators This section shows how a parser generator can be used to facilitate the constr ...

  6. bzoj1537

    dp+树状数组 一维排序,一维离散化,然后跑lis,其实就是一个二维偏序 #include<bits/stdc++.h> using namespace std; ; int dp[N], ...

  7. svchost.exe 占用内存过多

    http://www.tomshardware.com/forum/20583-63-svchost-netsvcs-speed By Lokesh Chandra: Just Go to Contr ...

  8. Rails5入门

    更新: 2017/05/29 更新: 2017/09/07 补充对ruby自身的扩张的放置位置  配置文件位置  /config/routes.rb  最简单的定义方法  get ('hello/in ...

  9. vs2017 + miniUI 后端框架使用

    vs2017 +  miniUI  后端框架使用 上miniUI官网直接下载框架.http://www.miniui.com/ 此框架使用说明很清楚. 2.1.vs2017创建安装miniUI后端框架 ...

  10. loadrunner11 安装破解,汉化包

    说说自己的心痛史,好不容易安装了loadrunner 11 居然浏览器不支持,我的系统是win8.1,ie浏览器最低支持ie11,我还能说啥子...其他浏览器试过了依旧是不可以!!所以我安装了一个虚拟 ...