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

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.

 
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. using namespace std;
  6. const int ms=;
  7. int a[ms];
  8. int n,p,cnt;
  9. void input()
  10. {
  11. scanf("%d",&n);
  12. for(int i=;i<n;i++)
  13. scanf("%d",&a[i]);
  14. }
  15.  
  16. void solve()
  17. {
  18. cnt=;
  19. int flag=a[n-];
  20. for(int i=n-;i>=;i--)
  21. {
  22. if(a[i]>flag)
  23. cnt++;
  24. else
  25. flag=a[i];
  26. }
  27. printf("Case #%d: %d\n",p++,cnt);
  28. }
  29. int main()
  30. {
  31. int T;
  32. scanf("%d",&T);
  33. p=;
  34. while(T--)
  35. {
  36. input();
  37. solve();
  38. }
  39. return ;
  40. }

K.Bro Sorting的更多相关文章

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

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

  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. K - K.Bro Sorting

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

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

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

  6. hdoj 5122 K.Bro Sorting 贪心

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

  7. K.Bro Sorting(思维题)

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

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

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

  9. hdu5122 K.Bro Sorting

    思路: 模拟. 实现: #include <iostream> #include <cstdio> using namespace std; ], n, t; int main ...

随机推荐

  1. 机器学习真的可以起作用吗?(3)(以二维PLA为例)

    前两篇文章已经完成了大部分的工作,这篇文章主要是讲VC bound和 VC dimension这两个概念. (一)前文的一点补充 根据前面的讨论,我们似乎只需要用来替代来源的M就可以了,但是实际公式却 ...

  2. 数往知来C#面向对象准备〈二〉

    面向对象(OOP→Object-Oriented Programming) 1.什么是面向对象? 一种分析问题的方式. 2.面向对象三大特征: 封装(隐蔽代码实现/复用/修改方便).继承.多态. 3. ...

  3. Windows 窗体—— 键盘输入工作原理

    方法 注释 PreFilterMessage 此方法在应用程序级截获排队的(也称为已发送的)Windows 消息. PreProcessMessage 此方法在 Windows 消息处理前在窗体和控件 ...

  4. RabbitMQ (三) 发布/订阅 -摘自网络

    这篇博客中,我们会做一些改变,就是把一个消息发给多个消费者,这种模式称之为发布/订阅(类似观察者模式). 为了验证这种模式,我们准备构建一个简单的日志系统.这个系统包含两类程序,一类程序发动日志,另一 ...

  5. 第二百八十天 how can I 坚持

    今天发现一只大bug,目前还没有解决掉... 晚上和徐斌还有他同学一块吃了个饭.还有.没了. 今天想早睡觉. 今天股市暴跌,二度熔断,好精彩,哈哈,不说啥了,还有苹果股票和谷歌市值越来越接近了,要走下 ...

  6. 从一个开发的角度看负载均衡和LVS(转)

    原文:http://blog.hesey.net/2013/02/introduce-to-load-balance-and-lvs-briefly.html 在大规模互联网应用中,负载均衡设备是必不 ...

  7. 几个代码片段-计算程序运行时间+获得当前目录+生成MD5

    计算程序运行时间 long startTime = System.currentTimeMillis(); System.out.println("程序运行时间: " + (Sys ...

  8. poj 2226 Muddy Fields(最小覆盖点+构图)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  9. C#的dll被其他程序调用时,获取此dll正确的物理路径

    当C# dll被其他程序调用时,用Application.StartupPath获取的dll路径并不一定是此dll的物理路径,有可能是调用程序的路径. 以下方法或者能够获取dll正确的物理路径(未经过 ...

  10. Algorithms Part 1-Question 6- 2SUM Median-数和以及中位数问题

    本次有两个编程问题,一个是求两个数的和满足一定值的数目,另一个是求中位数. 2SUM问题 问题描述 The goal of this problem is to implement a variant ...