K.Bro Sorting

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)

Total Submission(s): 571 Accepted Submission(s): 300

Problem Description

Matt’s friend K.Bro is an ACMer.

Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong order. The process repeats until no swap is needed.

Today, K.Bro comes up with a new algorithm and names it K.Bro Sorting.

There are many rounds in K.Bro Sorting. For each round, K.Bro chooses a number, and keeps swapping it with its next number while the next number is less than it. For example, if the sequence is “1 4 3 2 5”, and K.Bro chooses “4”, he will get “1 3 2 4 5” after this round. K.Bro Sorting is similar to Bubble sort, but it’s a randomized algorithm because K.Bro will choose a random number at the beginning of each round. K.Bro wants to know that, for a given sequence, how many rounds are needed to sort this sequence in the best situation. In other words, you should answer the minimal number of rounds needed to sort the sequence into ascending order. To simplify the problem, K.Bro promises that the sequence is a permutation of 1, 2, . . . , N .

Input

The first line contains only one integer T (T ≤ 200), which indicates the number of test cases. For each test case, the first line contains an integer N (1 ≤ N ≤ 106).

The second line contains N integers ai (1 ≤ ai ≤ N ), denoting the sequence K.Bro gives you.

The sum of N in all test cases would not exceed 3 × 106.

Output

For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of rounds needed to sort the sequence.

Sample Input

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

Sample Output

Case #1: 4 Case #2: 1

Hint

In the second sample, we choose “5” so that after the first round, sequence becomes “1 2 3 4 5”, and the algorithm completes.

题意

题意就是告诉你,某个人发明了一种新的排序算法,就是在这个序列中,随便选一个数,然后与后面与他相邻的数进行比较,如果大于后面的,就交换,直到不能交换为止

然后问你,这种操作最少需要多少次

题解

我们从后面开始找,假设最小值是最后一个数,然后让他与前面的比较,如果前面的数比他小的话,就更新最小值,否则就ans++

至于为什么,我们可以很容易证明,已经交换过的后面的序列,一定是从小到大排好了的,所以这样搞是可行的

吐槽

hdu用G++交的话,读入会很慢,然后T掉

代码

int a[maxn];
int main()
{
int t;
RD(t);
REP_1(ti,t)
{
int n;
RD(n);
REP(i,n)
RD(a[i]);
int ans=0;
int minn=a[n-1];
for(int i=n-2;i>=0;i--)
{
if(minn<a[i])
ans++;
else
minn=a[i];
}
printf("Case #%d: %d\n",ti,ans);
}
}

hdoj 5122 K.Bro Sorting 贪心的更多相关文章

  1. HDU 5122 K.Bro Sorting(模拟——思维题详解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...

  2. HDU 5122 K.Bro Sorting

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...

  3. 基础题:HDU 5122 K.Bro Sorting

    Matt's friend K.Bro is an ACMer.Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will ...

  4. HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...

  5. 树状数组--K.Bro Sorting

    题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/D Description Matt’s frie ...

  6. K.Bro Sorting

    Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total Submissio ...

  7. K - K.Bro Sorting

    Description Matt’s friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubb ...

  8. K.Bro Sorting(思维题)

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)T ...

  9. hdu 5122(2014ACM/ICPC亚洲区北京站) K题 K.Bro Sorting

    传送门 对于错想成lis的解法,提供一组反例 1 3 4 2 5同时对于这次案例也可以观察出解法:对于每一个数,如果存在比它小的数在它后面,它势必需要移动,因为只能小的数无法向右移动,而且每一次移动都 ...

随机推荐

  1. ModelState验证部分属性

    ModelState.Remove("Name") 去掉不需要验证的属性.

  2. 洛谷P3119 草鉴定

    这个题调了一天.. 传送门 读完题目之后我们不难想出这个题是个tarjan缩点问题,因为尽量多的经过草场,所以一号点所在的强连通分量里左右的点都是不需要在进行走逆向边,所能到达的. 然后问题就落在怎么 ...

  3. (四)Jsoup 获取 DOM 元素属性值

    第一节: Jsoup 获取 DOM 元素属性值 Jsoup获取DOM元素属性值 比如我们要获取博客的href属性值: 我们这时候就要用到Jsoup来获取属性的值 : 我们给下示例代码: package ...

  4. mac上Homebrew安装以及python安装

    Homebrew homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能. Homebrew常用命令 查看brew的帮助:brew –help 安装 ...

  5. asp.net MVC发布iis无法加载css,js和图片

    今天真够郁闷的,遇到了在本地能运行的项目到了iis服务器那里就不行了,无法加载css,js和图片,这里说清楚一下先,关于asp.net 的MVC中这样的情况其实不少,但是之前遇到的是在visual s ...

  6. jps出现process information unavailable的问题

    jps出现process information unavailable(当然我ps -aux了,确定该进程是存在的),网上查找了原因,我的理解是这样: 因为jps的进程信息是存储在/tmp/hspe ...

  7. 阿里云修改CentOS Linux服务器的主机名

    阿里云主机的默认主机名是为AY开头的随机名称,如何修改为易于区分的友好名称呢?请看下面的操作步骤: 1. vi /etc/hosts i键,修改主机名,esc键,:wq键保存退出 2. vi /etc ...

  8. Windows 服务器部署 asp.net core

    踩坑日记与 Windows 服务器部署 asp.net core 指南. 准备 操作系统:Windows Server 2008 R2 或更高版本 文件: Microsoft Visual C++ 2 ...

  9. idea在debug模式启动非常慢,日志正常debug模式一直在启动中状态

    背景 最近在使用idea开发工具时遇到了一个奇怪的现象,使用run tomcat项目可以正常启动,但是使用debug模式启动时,控制台可以正常打印日志,但是进度十分缓慢,也没有任何报出任何错误信息.只 ...

  10. CentOS7中热插拔硬盘如何读取新的硬盘

    在服务器或虚拟机上,一般会实现热插拔硬盘.此时CentOS7是无法读到新盘的,所以需要重新扫描. 我们添加一个新的硬盘演示: [root@xuexi ~]# ls /dev/sd* //应该还有一个s ...