hdu 5500 Reorder the Books(规律)
题意:
有一个1→n的排列形成的数列,我们要用最少的操作次数把这个数列从小到大排序,每次操作都是把一个数放到整个数列的最前面。
思路:
首先最大的数n是不用操作的(其他数操作好了,n自然在最后面了)。先找到数n的位置,在n之前找n-1,若没找到n-1,则n-1需要操作,所有小于n-1的数均需要操作;若找到了n-1,再接着往前依次找n-2,n-3,。。。假如数k找不到了,那就是至少需要k次操作。
复杂度O(n)。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n;
int a[N];
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
int ans=n;
int ed=n-; while(ed>=){
if(a[ed]==ans) ans--;
ed--;
}
printf("%d\n",ans);
}
return ;
}
hdu 5500 Reorder the Books(规律)的更多相关文章
- hdu 5500 Reorder the Books
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- 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 (水题)
题意: 有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(规律)
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Reorder the Books -- hdu -- 5500
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 4722 Good Numbers(规律题)
http://acm.hdu.edu.cn/showproblem.php?pid=4722 [题意]: 找GoodNumbers一个数N,如果它每一个位数字之和可以整除10,那么它就是GoodNum ...
随机推荐
- 通过项目逐步深入了解Mybatis<三>
Mybatis 高级知识 安排:对订单商品数据模型进行分析 订单商品数据模型 数据模型分析思路: 1.每张表记录的数据内容(分模块对每张表记录的内容进行熟悉,相当于学习系统需求的过程) 2.每张表重要 ...
- struts2采用convention-plugin实现零配置
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置. 配置文件精简了,的确是简便 ...
- 采用标准c进行目录文件遍历
图像处理的时候经常需要对一个目录的所有图像进行处理,遍历文件得c代码: 在windows中需要使用到宽字符. 另外,可以使用opencv封装的目录访问操作,下次给出. // DirTraverse.c ...
- Java核心技术,让计算机"一芯多用"的多线程技术
我们在使用计算的时候会感受到计算机好像在同时执行很多任务,这也是我最初接触计算机给我留下的印象,而我们普通人在同一时刻大脑只能思考一件事情(当然不排除一些异能者能够做到一心二用),而且我们在思考完一件 ...
- wpf msdn在线地址http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
- Excel中将时间格式转化成时间戳格式
时间戳转成正常日期的公式:C1=(A1+8*3600)/86400+70*365+19其中A1表示当时的1249488000时间戳数值其中C1就是所需的日期格式,C1单元格属性改成日期格式就可以了.正 ...
- JavaScript类的设计
[转载] Javascript与其他的面向对象语言不同,如C++,Java或PHP等.它并不是基于类的,而是基于原型的一种语言. 1.对象创建 在Javascript中创建一个类是非常容易的: var ...
- P - A + B(第二季水)
Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. ...
- u-boot基本命令
1.查看环境变量 printenv 2.网络相关命令 设置开发板ip:setenv ipaddr 192.168.2.110 设置子网掩码:setenv netmask 255.255.255.0 设 ...
- HDU-1049
Description An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u i ...