光荣的梦想(sequence.pas/in/out)
2011-10-12 14:39
225 查看
光荣的梦想
(sequence.pas/in/out)
Problem
Prince对他在这片大陆上维护的秩序感到满意,于是决定启程离开艾泽拉斯。在他动身之前,Prince决定赋予King_Bette最强大的能量以守护世界、保卫这里的平衡与和谐。在那个时代,平衡是个梦想。因为有很多奇异的物种拥有各种不稳定的能量,平衡瞬间即被打破。KB决定求助于你,帮助他完成这个梦想。
一串数列即表示一个世界的状态。
平衡是指这串数列以升序排列。而从一串无序数列到有序数列需要通过交换数列中的元素来实现。KB的能量只能交换相邻两个数字。他想知道他最少需要交换几次就能使数列有序。
Input
第一行为数据的组数N。对于每组数据,第一行为数列中数的个数n,第二行为n <= 10000个数。表示当前数列的状态。
Output
输出一个整数,表示最少需要交换几次能达到平衡状态。
Sample Input
4
2 1 4 3
Sample Output
2
==========================
这道题原来做过
是个归并
==================================
(sequence.pas/in/out)
Problem
Prince对他在这片大陆上维护的秩序感到满意,于是决定启程离开艾泽拉斯。在他动身之前,Prince决定赋予King_Bette最强大的能量以守护世界、保卫这里的平衡与和谐。在那个时代,平衡是个梦想。因为有很多奇异的物种拥有各种不稳定的能量,平衡瞬间即被打破。KB决定求助于你,帮助他完成这个梦想。
一串数列即表示一个世界的状态。
平衡是指这串数列以升序排列。而从一串无序数列到有序数列需要通过交换数列中的元素来实现。KB的能量只能交换相邻两个数字。他想知道他最少需要交换几次就能使数列有序。
Input
第一行为数据的组数N。对于每组数据,第一行为数列中数的个数n,第二行为n <= 10000个数。表示当前数列的状态。
Output
输出一个整数,表示最少需要交换几次能达到平衡状态。
Sample Input
4
2 1 4 3
Sample Output
2
==========================
这道题原来做过
是个归并
==================================
var n,t1:longint; ans:longint; a,b:array[1..10000]of longint; procedure init; begin assign(input,'sequence.in'); assign(output,'sequence.out'); reset(input); rewrite(output); end; procedure terminate; begin close(input); close(output); halt; end; procedure gb(x,y:longint); var mid:longint; i,j,k:longint; begin if x=y then exit; mid:=(x+y) shr 1; gb(x,mid); gb(mid+1,y); i:=x; j:=mid+1; k:=x; //这里的注意.. while k<=y do begin if (i<=mid)and(j<=y) then begin while (i<=mid)and(j<=y)and(a[i]<=a[j]) do //在前面比后面小的时候不交换.否则答案错误 begin b[k]:=a[i]; inc(i); inc(k); end; while (i<=mid)and(j<=y)and(a[i]>a[j]) do begin b[k]:=a[j]; inc(j); inc(k); ans:=ans+mid+1-i; end; end //在两段都有元素的情况下; else //在其中一段没有元素的情况下; begin while i<=mid do begin b[k]:=a[i]; inc(i); inc(k); end; while j<=y do begin b[k]:=a[j]; inc(j); inc(k); end; end; end; for i:=x to y do a[i]:=b[i]; end; procedure main; var i1:longint; i:longint; begin begin readln(n); for i:=1 to n do read(a[i]); ans:=0; gb(1,n); writeln(ans); end; end; begin init; main; terminate; end.
相关文章推荐
- 【枚举染色】嗅探器 sniffer.pas/c/cpp/in/out
- 外星生命(et.pas/in/out)
- 2012.10.3 子数整数(num.pas/in/out)
- 2012.10.3 阶乘问题(fact.pas/in/out)
- 2012.10.3 车厢重组(carry.pas/in/out)
- 2012.10.3 护卫队(convoy.pas/in/out)
- bzoj 3657 斐波那契数列(fib.cpp/pas/c/in/out)
- 【枚举染色】嗅探器 sniffer.pas/c/cpp/in/out
- 【动态规划】Tom的烦恼 tom.pas/c/cpp/in/out
- 【动态规划】Tom的烦恼 tom.pas/c/cpp/in/out
- 【字符串处理】文明的复兴 words.pas/c/cpp/in/out
- 【字符串处理】文明的复兴 words.pas/c/cpp/in/out
- 关押罪犯(prison.pas/cpp/in/out)
- 【字典序+栈】接龙游戏(words.pas/in/out)
- 异常-----freemarker.template.TemplateException: Expected collection or sequence. datas evaluated instead to freemarker.core.HashLiteral$SequenceHash on line 7, column 18 in inc/select.ftl.
- 创建带 IN 和 OUT 参数的 MySQL 存储过程
- 序列化(ObjectOutputStream和ObjectInputStream)(切记:out是输出到本地中,in是输入到程序中)
- Exception in thread "http-bio-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
- 测试工程师的光荣与梦想(三)
- This nav in addition to in search of your best check out can have also been never ever so simple in addition to at ease