hdoj5122K.Bro Sorting
来源:互联网 发布:云购源码 南京 编辑:程序博客网 时间:2024/05/05 04:33
K.Bro Sorting
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 1804 Accepted Submission(s): 826
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 .
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.
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
255 4 3 2 155 1 2 3 4
Sample Output
Case #1: 4Case #2: 1HintIn the second sample, we choose “5” so that after the first round, sequence becomes “1 2 3 4 5”, and the algorithm completes.
Source
2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int maxn=1000010;struct Node{int num,pos;}A[maxn];bool cmp(Node a,Node b){return a.num<b.num;}int main(){int i,j,k=1,n,m,t;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;++i){scanf("%d",&A[i].num);A[i-1].pos=i+1;}sort(A,A+n,cmp);int ans=A[0].pos-1,temp=0;for(i=1;i<n;++i){if(A[i].pos>A[temp].pos){ans+=A[i].pos-A[temp].pos-1;temp=i;}}printf("Case #%d: %d\n",k++,ans);}return 0;}
0 0
- hdoj5122K.Bro Sorting
- HDU5122 K.Bro Sorting
- K.Bro Sorting
- hdu_5122_K.Bro Sorting
- hdu5122 K.Bro Sorting
- hdu5122 K.Bro Sorting
- K.Bro Sorting HDU
- hdu 5122 K.Bro Sorting
- hdu 5122 K.Bro Sorting
- HDU 5122 K.Bro Sorting
- hdu 5122 K.Bro Sorting
- hdu5122K.Bro Sorting 树状数组
- HDU-5122-K.Bro Sorting
- HDU 5122 K.Bro Sorting
- HDU 5122 K.Bro Sorting
- HDU 5122 K.Bro Sorting
- HDU 5122 K.Bro Sorting
- HDU 5122 K.Bro Sorting
- What's Ailing ASP.NET Web Forms
- django搭建微信后台——批量获取用户信息
- 闲话ajax,例ajax轮询,ajax上传文件
- Angulars Controller作用域继承
- 《Windows 程序设计(第五版,珍藏版)》第I部分 关于图的那些事儿 学习总结
- hdoj5122K.Bro Sorting
- 设计模式系列9---和而不同的工厂方法模式
- 如何用PS制作动态下雪の效果
- GB2312编码
- Python学习笔记6-异常捕获取
- Swift学习之UIButton
- [BZOJ1010]玩具装箱
- IO_缓冲流_转换流_字节转为字符_乱码分析_编码与解码JAVA152-154
- 菜鸟初学android体验之——实现自定义简单标题栏的两种简单方法