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. XShell命令行使用

    1.建立连接: 2.查看总体目录: 3.查看对应服务目录: 4.删除对应jar包后,再查看目录下文件: 5.上传对应的jar文件: 6.重启服务 7.查看服务日志: 8.mv old-name new ...

  2. Java程序中的Log文件配置

    log4j.properties文件 log4j.rootLogger=info,stdout,logfile #stdout log4j.appender.stdout=org.apache.log ...

  3. python学习心得(三)

    一,面向对象编程 1,类和实例, class Student(object):#括号里面的是继承的类 def __init__(self, name, score):初始化对象时,参数个数 self. ...

  4. Angular 学习笔记——模块之间的通讯

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. RFS 理解

    1.背景 网卡接收一个数据包的情况下,会经过三个阶段:   - 网卡产生硬件中断通知CPU有包到达 - 通过软中断处理此数据包 - 在用户态程序处理此数据包   在SMP体系下,这三个阶段有可能在3个 ...

  6. C# Redis Server分布式缓存编程(一)

    这篇文章我将介绍如果用最简洁的方式配置Redis Server, 以及如何使用C#和它交互编程 一. 背景介绍 Redis是最快的key-value分布式缓存之一 缺点: 没有本地数据缓冲, 目前还没 ...

  7. Centos修改文件打开数限制

    一.查看系统限制最大打开数 cat /proc/sys/fs/file-max 还有一个问题是file-max最大能设置多大呢?一个经验算法是 256个fd 需4M内存.例如8G内存,8*1024/4 ...

  8. react-navigation-easy-helper

    本组件旨在不更改源码情况下,简单配置即可实现一些复杂的功能.如在任意位置进行跳转.根据路由名字返回指定页面.简化参数的获取.快速点击的拦截.统一页面跳转的拦截等. 安装: npm install re ...

  9. Sencha Test Futures API 探秘

    原文链接:http://blog.csdn.net/lovelyelfpop/article/details/52301249 英文原文:<Inside the Sencha Test Futu ...

  10. rsync详解之exclude排除文件(转)

    rsync详解之exclude排除文件 问题:如何避开同步指定的文件夹?  --excludersync  --exclude files and folders http://articles.sl ...