hdu 5500 Reorder the Books
http://acm.hdu.edu.cn/showproblem.php?pid=5500
Reorder the Books
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 942 Accepted Submission(s): 519
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.
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.
4
4 1 2 3
5
1 2 3 4 5
0
假设开始时所有的顺序都是乱的则乱码数为n(即需要移动的步数也为n),从最后一位往前找递减列
找到一个符合的递减序列的元素,乱码数就减少1
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ; int main()
{
int t, n, a[N];
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = ; i <= n ; i++)
scanf("%d", &a[i]);
int x = n;
for(int i = n ; i >= ; i--)
{
if(a[i] == x)
x--;//乱码数
}
printf("%d\n", x);
}
return ;
}
hdu 5500 Reorder the Books的更多相关文章
- 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的右边所有的书都必须抽 ...
- hdoj 5500 Reorder the Books
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- Reorder the Books -- hdu -- 5500
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- Reorder the Books(规律)
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- BestCoder Round #59 (div.2) B. Reorder the Books 想法题
Reorder the Books 问题描述 dxy家收藏了一套书,这套书叫<SDOI故事集>,<SDOI故事集>有n(n\leq 19)n(n≤19)本,每本书有一个编号,从 ...
- Reorder the Books-HDU5500
Problem Description dxy has a collection of a series of books called "The Stories of SDOI" ...
随机推荐
- linq to sql之like
contains——like '%提交%' StartsWith—— like '条件%' EndWith——like '%条件'
- 16进制string转成int
http://blog.csdn.net/wl1524520/article/details/25706521
- <Linux多线程服务端编程>学习记录
使用智能指针解决多线程下 类的解析冲突问题 有这样一个场景 使用StockFactory记录Stock的信息 容器是map<string,smart_ptr>; 代码如下: #inclu ...
- cxf soap rest webservice spring
1. 导入 jar 包 2. 编写接口 3. 编写实现 4. 配置spring 配置文件 5. 配置web.xml servlet 6. 访问 package com.diancai.test; im ...
- 12月6日 被引入的jsp 页面,引入 js 要注意结束符 要用 </script> 而不是 />
12月6日 被引入的jsp 页面,引入 js 要注意结束符 要用 </script> 而不是 />
- 3层+SVN学习笔记(2)
在对于餐桌付款程序设计时,需要先选中餐桌,然后点击付款.正常情况是这样的: 在程序设计时,没有考虑到用户未点击餐桌而直接进行付款的情况,程序出现以下错误: 在设计时,需要考虑用户未点击餐桌而直接进行付 ...
- 2018.09.26 bzoj5221: [Lydsy2017省队十连测]偏题(数学推导+矩阵快速幂)
传送门 由于没有考虑n<=1的情况T了很久啊. 这题很有意思啊. 考试的时候根本不会,骗了30分走人. 实际上变一个形就可以了. 推导过程有点繁杂. 直接粘题解上的请谅解. 不得不说这个推导很妙 ...
- 用org.mybatis.generator 生成代码
1:引入pom 2:增加生成配置xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- android apk签名原理
//这个md5跟腾讯的对应 public Signature getPackageSignature( ){ Context context=getContext(); String packageN ...
- Linux 修改 IP地址 和 网关
修改IP地址和网关是很常见的操作,在做相关实验的时候,如果没有设置好,会带来很多不必要的麻烦.. 1. 修改IP地址vi /etc/sysconfig/network-scripts/ifcfg-et ...