基础题:HDU 5122 K.Bro Sorting
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 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
Sample Input
2
5
5 4 3 2 1
5
5 1 2 3 4
Sample Output
Case #1: 4
Case #2: 1
像个脑筋急转弯。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int a[N];
int main(){
int T,cas=,n,ans,Min;
scanf("%d",&T);
while(T--){ scanf("%d",&n);
ans=;Min=n+;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=n;i>=;i--){
if(a[i]>Min)ans+=;
Min=min(a[i],Min);
}
printf("Case #%d: %d\n",++cas,ans);
}
return ;
}
基础题:HDU 5122 K.Bro Sorting的更多相关文章
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
- 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 ...
- 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 ...
- hdu 5122(2014ACM/ICPC亚洲区北京站) K题 K.Bro Sorting
传送门 对于错想成lis的解法,提供一组反例 1 3 4 2 5同时对于这次案例也可以观察出解法:对于每一个数,如果存在比它小的数在它后面,它势必需要移动,因为只能小的数无法向右移动,而且每一次移动都 ...
随机推荐
- jfinal不能正确加载html网页,总是报错的解决方法
今天自学jfinal,遇到一个很奇怪的问题,render("/index.html");总是报错. 仔细看错误日志,才发现原来是因为html网页放到了WEB-INF文件夹下面了,所 ...
- .Net Framework 开发Http协议
一.Http的基本原理 1.HTTP协议的运作方式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HTTP协议是基于请求/响应范式的.一个客户机与服务器建立连接后,发送一个请求 ...
- 14_Xml继承
[工程截图] [Person.java] package com.HigginCui; public class Person { private String name; public String ...
- OpenCV(6)-腐蚀和膨胀
腐蚀和膨胀属于形态学操作. 腐蚀和膨胀 腐蚀是指:将卷积核B滑过图像A,找出卷积核区域内最小像素值作为锚点像素值.这一操作可以扩大低像素值区域. 膨胀是指:将卷积核B滑过图像A,找出卷积核区域内最大像 ...
- Linux Master/Baremetal Remote 配置下的裸机调试
为了实现在ZC702开发板上的两颗Cortex-A9处理器上实现Linux Master/Baremetal Remote 配置,并对Remote端的裸机程序进行调试,需要注意的几点如下: 一.建立p ...
- uploadify 上传文件出现HTTP 404错误
今天在使用jquery.uploadify.js上传文件的时候,出现HTTP 404错误,此错误在上传较小文件时不会出现,在上传一个50M左右文件时出现此错误,经过测试和日志查看发现,根本没有进入后台 ...
- css3怎么隐藏dom:4种方法
1.display:none; 2.position:absolute; left:-99999px; 3.visibility:hidden; 4.opacity:0;
- php快递单号查询源码
贴下记录下php查询快递单号的源码,能查询各种快递的快递单号记录,中通.圆通快递.顺丰快递等都不是问题.只需要在 爱快递(www.aikuaidi.cn)上面申请一个快递key即可,下面把源码分享下, ...
- Laravel 5.1 事件、事件监听的简单应用
有时候当我们单纯的看 Laravel 手册的时候会有一些疑惑,比如说系统服务下的授权和事件,这些功能服务的应用场景是什么,其实如果没有经历过一定的开发经验有这些疑惑是很正常的事情,但是当我们在工作 ...
- yii 缓存探究
1.在配置文件中 //在权威指南上是'cache' 其实可以根据不同的缓存组件起不同的名称 //memcache缓存 'memcache' => array( 'class' => 'sy ...