K.Bro Sorting

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 3072    Accepted Submission(s): 1390

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.

Source

2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)

//题意:N 个数,要排序,用一种特殊的方法排,可以任意选择一个数,将它与右边的比较,如果大于右边的,就向右移动,直到小于。这样算移动一次,问用这种方法排序,最少要几次将数字排好序。

题解:从后向前遍历,如果比后面某个数要大,则说明一定需要一次来移动

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MX 1000005
int n;
int num[MX]; int main()
{
int T;
cin>>T;
for (int cnt=;cnt<=T;cnt++)
{
scanf("%d",&n);
for (int i=;i<=n;i++)
scanf("%d",&num[i]);
int ans =;
int mmm = num[n];
for (int i=n-;i>=;i--)
{
if (num[i]>mmm)
ans++;
else
mmm = num[i];
}
printf("Case #%d: %d\n",cnt,ans);
}
return ;
}

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

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

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

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

  4. HDU 5122 K.Bro Sorting

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

  5. K.Bro Sorting

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

  6. K - K.Bro Sorting

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

  7. hdoj 5122 K.Bro Sorting 贪心

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

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

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

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

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

随机推荐

  1. 【AS3 Coder】任务五:Flash 2D游戏的第二春(下)

    在上一节中,我们基本上已经讲完了游戏中最主要的逻辑部分,不过为了更加全面地运用Starling中的一些特性,在本节中我们将一起来看看如何实现多面板切换以及粒子效果,这两个玩意儿可是比较频繁会出现于St ...

  2. [转载]Process工具类,提供设置timeout功能

    FROM:http://segmentfault.com/blog/lidonghao/1190000000372535 在前一篇博文中,简单介绍了如何使用Process类来调用命令行的功能,那样使用 ...

  3. PHP empty()函数说明---用了N遍了就是记不住

    从表面上看,很容易误解empty()函数是判断字符串是否为空的函数,其实并不是,我也因此吃了很多亏. empty()函数是用来测试变量是否已经配置.若变量已存在.非空字符串或者非零,则返回 false ...

  4. 前端存储之indexedDB

    在前一个阶段的工作中,项目组要开发一个平台,为了做出更好的用户体验,实现快速.高质量的交互,从而更快得到用户的反馈,要求在前端把数据存储起来,之后我去研究了下现在比较流行的前端存储数据库,找到了ind ...

  5. 持久化配置管理 diamond 使用简介

    本次为大家介绍diamond的概况和快速使用. 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠.易用,目前淘宝内部绝大多数系统的配置,由diamond来进行统一管 ...

  6. Win7如何开启Messenger服务

    1 如图所示,在WIN7系统中没有找到Messenger这个服务.因为Messenger在Windows 7/Server 2008 R2里都去掉了   2 而在XP系统中,开启了Messenger服 ...

  7. 微信java开发之实现微信主动推送消息

    1.拉取access_token2.拉取用户信息3.主动推送消息4.接口貌似要申请权限5.依赖httpclient4.2.3 和jackson 2.2.1 public class WeixinAPI ...

  8. My sql 实用教程

    http://wenku.baidu.com/link?url=uwWWeGTZU61MQSSArf2pYRd4jPd7k7gNsx75KxEUKO1MlMLAAFiIF-fus3CY4RLyyzbZ ...

  9. view无限旋转

    - (void) showRefreshAnimation { [UIView animateWithDuration: options:UIViewAnimationOptionCurveLinea ...

  10. 清除掉AD的相关属性!

    今天有朋友问我怎么清除掉AD 的相关属性,由于他们的用户都设置了登录到属性,这样我们的用户就仅仅能登陆他须要设置的计算机.对于兴许规则的变更的时候,我们的管理员配置起来就比較复杂.他须要非常长的时间去 ...