BestCoder Round 59 (HDOJ 5500) Reorder the Books
Problem Description
dxy has a collection of a series of books called “The Stories of SDOI”,There are n(n≤19) books in this series.Every book has a number from 1 to n.
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) in the first line standing for the number of testcases.
For each testcase, there is an positive integer n in the first line standing for the number of books in this series.
Followed n positive integers separated by space standing for the order of the disordered books,the ith integer stands for the ith book’s number(from top to bottom).
Hint:
For the first testcase:Moving in the order of book3,book2,book1 ,(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
Code
#include <iostream>
using namespace std;
int main()
{
int T; cin >> T;
int book[20];
while (T--)
{
int n; cin >> n;
int index, number = n;
for (int i = 0; i < n; i++)
{
cin >> book[i];
if (book[i] == n) index = i;
}
for (int i = index; i >= 0; i--)
{
if (book[i] == number) number--;
}
cout << number << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
BestCoder Round 59 (HDOJ 5500) Reorder the Books的更多相关文章
- hdoj 5500 Reorder the Books
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu 5500 Reorder the Books
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- BestCoder Round #59 (div.2) B. Reorder the Books 想法题
Reorder the Books 问题描述 dxy家收藏了一套书,这套书叫<SDOI故事集>,<SDOI故事集>有n(n\leq 19)n(n≤19)本,每本书有一个编号,从 ...
- HDU 5500 Reorder the Books 贪心
Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- hdu 5500 Reorder the Books(规律)
题意: 有一个1→n的排列形成的数列,我们要用最少的操作次数把这个数列从小到大排序,每次操作都是把一个数放到整个数列的最前面. 思路: 首先最大的数n是不用操作的(其他数操作好了,n ...
- HDU 5500 Reorder the Books (水题)
题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...
- HDU5501/BestCoder Round #59 (div.2)The Highest Mark dp+贪心
The Highest Mark 问题描述 2045年的SD省队选拔,赛制和三十年前已是完全不同.一场比赛的比赛时间有 tt 分钟,有 nn 道题目. 第 ii 道题目的初始分值为 A_i(A_i \ ...
- Reorder the Books -- hdu -- 5500
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- Phalcon 的 bootstrap.php 自动加载完成;非常人性化的设计
<?php /** * Bootstraps the application */ use Phalcon\DI\FactoryDefault as PhDi, Phalcon\Config a ...
- srpm包的编译方式
基本说明:后缀仅为rpm的包如xxxxx.rpm称作为二进制包 ------ 可以直接安装到架构匹配的系统上; 后缀为src.rpm的包如webkitgtk-2.4.7-1.fc21.src.rpm称 ...
- Autoit实现分割字符串,循环输出元素
#include <MsgBoxConstants.au3> Local $aDays = StringSplit("Mon,Tues,Wed,Thur,Fri,Sat,Sun& ...
- 转:在虚拟机中用NAT方式连接网络
1.安装VMware Workstation .在安装过VMware Workstation软件后,会在本地连接中,多了两个虚拟网卡,一个是 VMware Network Adapter for VM ...
- 一步一步学习SignalR进行实时通信_7_非代理
原文:一步一步学习SignalR进行实时通信_7_非代理 一步一步学习SignalR进行实时通信\_7_非代理 SignalR 一步一步学习SignalR进行实时通信_7_非代理 前言 代理与非代理 ...
- LD1-K(求差值最小的生成树)
题目链接 /* *题目大意: *一个简单图,n个点,m条边; *要求一颗生成树,使得其最大边与最小边的差值是所有生成树中最小的,输出最小的那个差值; *算法分析: *枚举最小边,用kruskal求生成 ...
- 深入JS系列学习4
深入JS系列学习4 Javascript 装载和执行 明白了JS的装载和执行,没有给出很好的解决方案,在IE下可用defer属性: 浏览器对于Javascript的运行有两大特性:1)载入后马上执行, ...
- HDOJ(HDU) 2103 Family planning(需要注意范围)
Problem Description As far as we known,there are so many people in this world,expecially in china.Bu ...
- Hive学习笔记【转载】
本文转载自:http://blog.csdn.net/haojun186/article/details/7977565 1. HIVE结构 Hive 是建立在 Hadoop 上的数据仓库基础构架. ...
- HDU5032 -- Always Cook Mushroom 树状数组 14年北京网络赛
题意:1000*1000的格子, 坐标为(1, 1) ~ (1000, 1000), 常数 A, B, 点(x, y)权值为 (x + A) * (y + B), q次询问, 每次询问(0, 0) ...