hdoj 5122 K.Bro Sorting 贪心
K.Bro Sorting
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 571 Accepted Submission(s): 300
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.
题意
题意就是告诉你,某个人发明了一种新的排序算法,就是在这个序列中,随便选一个数,然后与后面与他相邻的数进行比较,如果大于后面的,就交换,直到不能交换为止
然后问你,这种操作最少需要多少次
题解
我们从后面开始找,假设最小值是最后一个数,然后让他与前面的比较,如果前面的数比他小的话,就更新最小值,否则就ans++
至于为什么,我们可以很容易证明,已经交换过的后面的序列,一定是从小到大排好了的,所以这样搞是可行的
吐槽
hdu用G++交的话,读入会很慢,然后T掉
代码
int a[maxn];
int main()
{
int t;
RD(t);
REP_1(ti,t)
{
int n;
RD(n);
REP(i,n)
RD(a[i]);
int ans=0;
int minn=a[n-1];
for(int i=n-2;i>=0;i--)
{
if(minn<a[i])
ans++;
else
minn=a[i];
}
printf("Case #%d: %d\n",ti,ans);
}
}
hdoj 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
Matt's friend K.Bro is an ACMer.Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will ...
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...
- 树状数组--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 ...
- K.Bro Sorting(思维题)
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)T ...
- hdu 5122(2014ACM/ICPC亚洲区北京站) K题 K.Bro Sorting
传送门 对于错想成lis的解法,提供一组反例 1 3 4 2 5同时对于这次案例也可以观察出解法:对于每一个数,如果存在比它小的数在它后面,它势必需要移动,因为只能小的数无法向右移动,而且每一次移动都 ...
随机推荐
- accept系统调用
/* * For accept, we attempt to create a new socket, set up the link * with the client, wake up the c ...
- aarch64_g5
gtkmm24-devel-2.24.5-2.fc26.aarch64.rpm 2017-02-11 18:17 620K fedora Mirroring Project gtkmm24-docs- ...
- swagger学习
https://segmentfault.com/a/1190000010144742 https://segmentfault.com/a/1190000014775124 https://blog ...
- SQL 根据关联表更新主表中字段数据
今天遇到一个客户的数据更新问题,两个相关联的表,一个主表用于保存单据主要信息,一个副表用于保存单据的明细信息:现在要把主表的其中一个字段的数据更新到副表的一个字段中保存.精通的SQL语法的,当然是很简 ...
- 数据库-mysql函数
一:MySQL中提供了许多内置函数 CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENGTH()返 ...
- centos7.2下caffe的安装及编译
1.前期准备 安装依赖 sudo yum install protobuf-devel leveldb-devel snappy-devel opencv-devel boost-devel hdf5 ...
- Cocos2d-x for Windows Phone 用法总结
鉴于诺基亚(微软移动这个没人用的手机)开发者比较少,cocos2dx移植方面更是少的问题,总结一下WP8移植方面的资料,希望对大家有用,自己也当作笔记留念. 1.WP8方面有两种方式创建项目,Hell ...
- MySQL慢查询优化
MySQL数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在大量数据进行比对或聚合时.磁盘I/O瓶颈发生在装入数据远大于内存容量的时候,如果应用分布在网络上,那么查询量相当大的 ...
- Python全栈开发之12、html
从今天开始,本系列的文章会开始讲前端,从htnl,css,js等,关于python基础的知识可以看我前面的博文,至于python web框架的知识会在前端学习完后开始更新. 一.html相关概念 ht ...
- Educational Codeforces Round 9 D - Longest Subsequence
D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...