题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5122

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
 
题意描述:
输入数组元素的个数N(1 ≤ N ≤ 106)
计算并输出按下面的规则进行操作,最少需要进行几轮这样的操作使得数组中的元素按升序排列
规则:
对于每个数来说,如果它大于等于它右边的数,则交换两数的位置,直到小于右边的数或者到了数组尾部。将该数“归位”的一系列操作成为一轮。比如题中所说的“1 4 3 2 5”,将4归位后“1 3 2 4 5”,称为一轮操作。
解题思路:
先说结论,不清楚的再看详解。
结论:
从后向前遍历,计算比当前最小值大的数的个数。
详解:
      首先,我们可以发现如果一个数的后面存在一个小于它的数,就必须要对它进行一轮操作,这个没有疑问,关键是进行完这一轮操作后该数是否已经彻底归位,换句话说,有可能存在对其进行二次操作(甚至是多次),例如,将3 1 4 5 2中的3归位,变成了1 3 4 5 2,很显然,这一轮操作的归位是不彻底的,因为3归位到2的后面才是彻底的归位。
      那么怎么才能避免二次操作,就是问题的精髓所在了。还拿上面的例子为例,3归位到4之前1之后的判断依据是3比它之前的数都大,那是否需要继续进行交换(也是是否需要进行二次操作的关键)的判断依据是3后面的数中的有没有比3还小的,换句话说,3是否比它之后所有数中的最小数大,如果是,那么将该轮操作才算执行彻底;如果不是就证明3归位彻底,不需要进行二次操作,从而达到使得操作轮数最少的目的。
  看到这,想必都在想怎么求得某个数后面的最小数呢,可以弄一个循环,遍历它之后的每个数,找到最小数,很直接的告诉你,会超时的,数据规模是一百万,两层循环就超时了。更巧妙的做法是,从后向前遍历,边走边找最小数,先假设a[n]为最小数,先前遍历,如果有比当前最小数大的数,直接计数,否则说明最小数需要更新了,更新最小数,继续想前找,直到找完。
  问题很简单,AC代码也很短,但是如何能省时,省力的解决问题,才是竞赛最令人兴奋的地方。
AC代码:
 #include<stdio.h>
int a[];
int main()
{
int T,n,ans,t,i,min_num;
scanf("%d",&T);
for(t=;t<=T;t++)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
min_num=a[n];
ans=;
for(i=n-;i>=;i--)
{
if(min_num < a[i])
ans++;
else
min_num=a[i];
}
printf("Case #%d: %d\n",t,ans);
}
return ;
}
 
 
 

HDU 5122 K.Bro Sorting(模拟——思维题详解)的更多相关文章

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

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

  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(2014北京区域赛现场赛K题 模拟)

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

  4. hdoj 5122 K.Bro Sorting 贪心

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

  5. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. K.Bro Sorting(思维题)

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

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

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

  8. K.Bro Sorting

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

  9. K - K.Bro Sorting

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

随机推荐

  1. LAMP第三部分php,mysql配置

    php配置 1. 配置disable_functiondisable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshell ...

  2. Linux(CentOS6.5)下编译Popt报错”GNU gettext is required. The latest version”(gettext已经编译安装,但是没有安装在默认目录)的解决方案

    本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢!   背景: 编译popt的时候出现下述报错. 直接vi查看confi ...

  3. 佛祖保佑永无bug的源代码

    ${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIGHT_RED}_ooOoo_${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIG ...

  4. MySQL集群PXC的搭建

    MySQL集群PXC的搭建 最近公司某客户要求我们的数据库搭建PXC集群以保证他们的系统高性能和搞稳定性 以后花费了一些时间去搭建和测试,也踩过一些坑,准备分享出来 系统:centos6.6PXC:5 ...

  5. php gif处理

    http://www.jb51.net/article/51702.htm https://github.com/Sybio/GifCreator https://github.com/Sybio/G ...

  6. Ant Design Pro 学习二 新建菜单-布局

    新建布局,注意格式: src/common/nav.js 中增加 { component: dynamicWrapper(app, [], () => import('/path/to/NewL ...

  7. css布局--水平居中

    一.水平居中 1. 使用text-align和display:inline-block实现水平居中 html <div class="parent"> <div ...

  8. Linux下设置SSH端口

    SSH 为 Secure Shell的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话 ...

  9. VMware仅主机模式虚拟机无法ping通物理机

    问题描述 在VMware Workstation中新建了一个虚拟机CentOS7,网络适配器选择的是"仅主机模式",结果,物理机ping不通虚拟机,虚拟机也ping不通物理机. 原 ...

  10. Qt 5中信号和槽的新语法

    QT 是一个跨平台的 C++ GUI 应用构架,它提供了丰富的窗口部件集,具有面向对象.易于扩展.真正的组件编程等特点,更为引人注目的是目前 Linux 上最为流行的 KDE 桌面环境就是建立在 QT ...