【HDU - 5500】 Reorder the Books (思维)

题干:

dxy has a collection of a series of books called "The Stories of SDOI",There are n(n≤19)n(n≤19) books in this series.Every book has a number from 11 to nn. 

dxy puts these books in a book stack with the order of their numbers increasing from top to bottom. dxy takes great care of these books and no one is allowed to touch them. 

One day Evensgn visited dxy's home, because dxy was dating with his girlfriend, dxy let Evensgn stay at home himself. Evensgn was curious about this series of books.So he took a look at them. He found out there was a story about "Little E&Little Q". While losing himself in the story,he disrupted the order of the books. 

Knowing that dxy would be back soon,Evensgn needed to get the books ordered again.But because the books were too heavy.The only thing Evensgn could do was to take out a book from the book stack and and put it at the stack top. 

Give you the order of the disordered books.Could you calculate the minimum steps Evensgn would use to reorder the books? If you could solve the problem for him,he will give you a signed book "The Stories of SDOI 9: The Story of Little E" as a gift. 

Input

There are several testcases. 

There is an positive integer T(T≤30)T(T≤30) in the first line standing for the number of testcases. 

For each testcase, there is an positive integer nn in the first line standing for the number of books in this series. 

Followed nn positive integers separated by space standing for the order of the disordered books,the ithith integer stands for the ithith book's number(from top to bottom). 


Hint: 
For the first testcase:Moving in the order of book3,book2,book1book3,book2,book1 ,(4,1,2,3)→(3,4,1,2)→(2,3,4,1)→(1,2,3,4)(4,1,2,3)→(3,4,1,2)→(2,3,4,1)→(1,2,3,4),and this is the best way to reorder the books. 
For the second testcase:It's already ordered so there is no operation needed.

Output

For each testcase,output one line for an integer standing for the minimum steps Evensgn would use to reorder the books.

Sample Input

2
4
4 1 2 3
5
1 2 3 4 5

Sample Output

3
0

 

题目大意:

   有n本带编号的书,现在给你打乱了顺序,定义一次操作可以任选一本书并且放到最顶部。问你最少需要多少次操作可以将这一摞书调整为有序(编号从小到大)?

解题报告:

     直接模拟就行了。。先找出来需要移动多少本书,假设是x本,那么肯定可以通过x次操作还原回来。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int ji,ou,all,n;
int a[MAX];
int main()
{int t;cin>>t;while(t--) {cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i);int now = n,ans=0;for(int i = n; i>=1; i--) {if(now == a[i]) now--;else ans++;}printf("%d\n",ans);} return 0 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440942.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

github怎么切换到gitee_AOSP-RISCV 的开源仓库在 Gitee 上新建了镜像

前阵子在知乎上给大家介绍了我们在移植 AOSP 到 RISC-V 上的第一步: 汪辰&#xff1a;第一个 RISC-V 上的“Android 最小系统”​zhuanlan.zhihu.com目前所有的工作成果都是开源在 Github 上的&#xff0c;移植改动涉及的子仓库达到 9 个&#xff0c;所有源码下载下来达到 537M…

联想微型计算机拆,联想10064一体机拆机,联想一体机硬盘怎么拆

电脑现在在我们生活中有着非常广泛的用途&#xff0c;不论是我们的工作还是学习都离不开电脑的辅助。我们工作时可以用电脑进行记录档案资料&#xff0c;学习时可以用电脑查阅资料&#xff0c;及时解决一些学习上遇到的疑问。有了电脑之后我们的工作学习效率都得到了非常大的提…

微型计算机原理中断实验,微机原理实验---中断控制实验.doc

微机原理实验---中断控制实验深 圳 大 学 实 验 报 告课程名称&#xff1a; 微机计算机设计实验项目名称&#xff1a; 8259 中断控制实验学院&#xff1a; 信息工程学院专业&#xff1a; 电子信息工程指导教师&#xff1a;报告人&#xff1a; 学号&#xff1a; 2009100000班级&…

【CodeForces - 510D】Fox And Jumping(dp,stlmap,数论的性质)

题干&#xff1a; Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: len…

db2 linux 平台下迁移_Linux 下的 DB2数据库的迁移

前言&#xff1a;DB2 Universal Database™(DB2 UDB) 有一对非常有用的工具&#xff0c;可以帮助您实现这种跨平台的备份与恢复功能。 db2move 工具利用了 DB2 的数据移动工具(export 和 import 或 load)来移动数据库表。然而&#xff0c;由于数据库的内容远远不止于用户表&…

加拿大红河学院计算机专业,加拿大红河学院

加拿大红河学院是加拿大曼省最大的由曼省财政支持的公立高等专科学院&#xff0c;是曼省规模最大、课程最全面的学院&#xff0c;也是加拿大最大的学院之一。近年来&#xff0c;加拿大红河学院受到国人狂热追捧&#xff0c;申请人数逐年增长。加拿大红河学院专长于商业管理与国…

【CodeForces - 471C】MUH and House of Cards (思维,找规律)

题干&#xff1a; Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that theyve already found a hefty deck of n playing cards. Lets describe the house they want t…

哪个读书app可以导入txt_QQ阅读iphone版怎么导入电子书 三种手机QQ阅读器导入本地图书图文教程...

QQ阅读iphone版是一款比较方便的移动终端阅读软件&#xff0c;除了从电子书城下载或者购买电子书外&#xff0c;我们也可以把自己电脑中的电子书上传到QQ阅读软件中&#xff0c;不过果粉们都知道苹果手机没有文件管理器&#xff0c;无法使用qq阅读直接点击查看本地小说&#xf…

【CodeForces - 471B】MUH and Important Things (模拟,细节)

题干&#xff1a; Its time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are ntasks for the day and each animal should do each of these tasks. For each task, t…

大一计算机绩点3算什么水平,绩点只有3?我可以解释一下

放张图文无关压压惊最近开始申请学业奖学金了&#xff0c;大家开始计算自己的绩点&#xff0c;我也算了一下自己的成绩&#xff0c;结果是比3多一点点(如果没有算错的话)。我觉得这是一个比较合适的数字&#xff0c;没有比3小已经很满足了&#xff0c;毕竟学的并不好&#xff0…

不能用了 重装系统git_怎么用光盘重装系统?

身边没有U盘&#xff0c;电脑无法进入操作系统&#xff0c;只有一个系统光盘如何给电脑重装系统呢&#xff1f;受条件限制不能通过小白在线安装和U盘重装&#xff0c;今天教大家怎么用光盘重装系统吧。光盘重装系统准备工作1、保证电脑带有光驱功能&#xff0c;并且光驱处于正常…

【CodeForces - 472A】Design Tutorial: Learn from Math (tricks,思维,数论,打表)

题干&#xff1a; One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that. For example, there is a statement called the "Goldbachs conject…

浙江经济职业技术学院计算机排名,浙江经济职业技术学院排名第几

关于浙江经济职业技术学院排名的问题考生问&#xff1a; 关于浙江经济职业技术学院的排名&#xff0c;我想抛给小编姐姐几个问题哦。一、浙江经济职业技术学院今年排名第几&#xff1f;对&#xff0c;指的是全国千余所专科院校当中的排名哦&#xff1b;二、浙江经济职业技术学院…

webform计算某几列结果_WebForm获取checkbox选中的值(几个简单的示例)

PS&#xff1a;最近在做权限管理这个模块&#xff0c;发现用checkbox的地方挺多的&#xff0c;于是写了个简单的例子&#xff0c;以供以后学习和使用。1.前端页面&#xff1a;张三李四王五赵六孙琦猪八2.后台方法&#xff1a;#region 获取从前端页面回传过来的 CheckBox 的值 v…

东明县计算机学校,东明县职业中等专业学校2021年招生信息

一、2021年东明县职业中等专业学校招生计划(一) 职教高考提前批招生计划学校代码专业名称招收人数学制学费备注说明863化学工艺40三年免学费从2022年起&#xff0c;山东省开始实施“职教高考”制度&#xff0c;采 取“文化素质职业技能”考试招生办法&#xff0c;职教高考的 本…

python去除图像光照不均匀_低光照环境下图像增强相关

Low-Light Image Enhancement via a Deep Hybrid Network [TIP2019]Underexposed Photo Enhancement using Deep Illumination Estimation[CVPR2019]---------Low-Light Image Enhancement via a Deep Hybrid Network [TIP2019]作者提出一个混合的网络来同时学习内容&#xff0…

【CodeForces - 523C】Name Quest (模拟)

题干&#xff1a; A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or mo…

w10计算机无法打印,老司机解答win10系统电脑无法打印的详细技巧

大家在使用电脑工作的时候会遇到win10系统电脑无法打印的问题&#xff0c;于是就有一些朋友到本站咨询win10系统电脑无法打印问题的解决步骤。解决win10系统电脑无法打印的问题非常简单&#xff0c;只需要你依照1、请确保打印机已打开并连接到你的电脑。 2、如果仍然无法工作&…

vb外部调用autocad_AutoCAD教程之图块的各种相关操作和概念

制图过程中&#xff0c;有时常需要插入某些特殊符号供图形中使用&#xff0c;此时就需要运用到图块及图块属性功能。利用图块与属性功能绘图&#xff0c;可以有效地提高作图效率与绘图质量。也是绘制复杂图形的重要组成部分。一、图块的特点图块是一组图形实体的总称&#xff0…

【CodeForces - 520C】DNA Alignment (快速幂,思维)

题干&#xff1a; Vasya became interested in bioinformatics. Hes going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Lets assume that strings s and t have the same l…