HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5122
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 .
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.
In the second sample, we choose “5” so that after the first round, sequence becomes “1 2 3 4 5”, and the algorithm completes.
#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(模拟——思维题详解)的更多相关文章
- 基础题:HDU 5122 K.Bro Sorting
Matt's friend K.Bro is an ACMer.Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will ...
- HDU 5122 K.Bro Sorting
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...
- hdoj 5122 K.Bro Sorting 贪心
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- 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 ...
- K.Bro Sorting(思维题)
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)T ...
- 树状数组--K.Bro Sorting
题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/D Description Matt’s frie ...
- K.Bro Sorting
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submissio ...
- K - K.Bro Sorting
Description Matt’s friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubb ...
随机推荐
- 用HTML5和原生js实现放大局部图片
drawImage方法 context.drawImage(image,sx,sy,sh,dx,dy,dw,dh) sx\sy起始图像的横纵坐标,sh\sd起始图像的大小,dx\dy复制图像的横纵坐标 ...
- java方向及学习方法
随笔:由于回首最近刚刚上班的缘故,平时基本没时间上播客了,所以回首会定期的抽时间分享一些干货给朋友们,就是周期不会像之前那么频繁了.最近有朋友跟回首说想没事儿的时候自学Java,但苦于不知道怎么去学, ...
- VS2010安装OpenGL
以下涉及到的所有资源都在这里: 链接:https://pan.baidu.com/s/1eSctT5K 密码:174s *我的VS2010的安装位置:D:\Program Files (x86)\M ...
- Xamarin.android Activity动画切换效果实现
http://blog.csdn.net/esunshine1985/article/details/44302903 1.在Resources--values下新建styles.xml,添加内容如下 ...
- 关于linux命令ssh的总结
因为项目计算量比较大,需要将任务分布到多台电脑上面运行,因为对于分布式概念不熟,就想到了linux最简单的ssh协议,远程控制其他电脑,然后写shell脚本统一在所有电脑上运行程序.(我的操作系统为U ...
- (class file version 53.0), Java Runtime versions up to 52.0错误的解决方法
遇到这个错误是在Apache Tomcat上部署应用程序的时候遇到的,具体的错误描述是: java.lang.UnsupportedClassVersionError: HelloWorld has ...
- SVG 入门——理解viewport,viewbox,preserveAspectRatio
工欲善其事必先利其器,没有真正搞懂SVG里的viewport,viewbox, preserveAspectRatio这三个属性,就很容易遇到坑,最近写项目用到svg这三个属性被我一眼就略过 ,后来发 ...
- scrapy使用PhantomJS爬取数据
环境:python2.7+scrapy+selenium+PhantomJS 内容:测试scrapy+PhantomJS 爬去内容:涉及到js加载更多的页面 原理:配置文件打开中间件+修改proces ...
- Web服务器(Apache)与Servlet容器(Tomcat)
之前一直比较迷惑Apache与Tomcat的关系,通过查询资料,有所了解,现记录于此. Apache与Tomcat 两者定位:Apache是HTTP Web服务器,Tomcat是Web容器. 有一个非 ...
- IDEA第六章----快捷键
第一节:解决快捷键冲突 idea支持很多快捷键,这样就导致了很多快捷键和其他应用冲突,所以需要把其他应用的快捷键去掉,下面以输入法和QQ为例. QQ我就留下了提取消息和截图,这个是个人习惯问题. 第二 ...