合并果子解题报告.doc_第1页
合并果子解题报告.doc_第2页
合并果子解题报告.doc_第3页
合并果子解题报告.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

合并果子解题报告1. 首先的思路是先排序,然后找到两个最小的,插入排序到原来的序列中,速度很慢var a:array0.10000 of longint; n,total:longint; i:integer;procedure swap(var a,b:longint);var t:integer;begint:=a; a:=b; b:=tend;procedure quick_sort(m,n:integer);var i,j,x:integer;begini:=m; j:=n; x:=a(i+j) div 2;repeat while aix do inc(i); while xaj do dec(j); if ij;if mj then quick_sort(m,j);if in then quick_sort(i,n);end;procedure insert_sort(n:integer);var i,j:integer;begini:=n-1; a0:=an;while aia0 do begin ai+1:=ai; dec(i)end;ai+1:=a0end;beginreadln(n);for i:=1 to n do read(ai);quick_sort(1,n);for i:=1 to n-1 do begin an-i:=an-i+an-i+1; an-i+1:=0; inc(total,an-i); insert_sort(n-i)end;writeln(total);end.2. 我们可以通过堆排序找最小值var a:array0.20000 of longint;i,j,n,ed,sum,t:longint;procedure heapdown(i,n:longint);var j,t:longint;beginj:=i*2;while j=n do begin if (jn) and (aj+1aj then begin t:=ai; ai:=aj; aj:=t; i:=j; j:=i*2; end else j:=maxlongint; end;end;beginreadln(n);for i:=1 to n do read(ai);for i:=n div 2 downto 1 do heapdown(i,n);sum:=0;while n1 do begin t:=an;an:=a1;a1:=t; heapdown(1,n-1); dec(n); t:=an;an:=a1;a1:=t; heapdown(1,n-1); an:=an+an+1; sum:=sum+an; end;writeln(sum);end.3. 我们可以通过维护两个单调队列来完成vartot:array0.20000 of longint;quea,queb:array0.10000 of longint;i,j,n,la,lb,ra,rb,x,max,k,a1,a2,b1,b2,sum:longint;procedure minx;var i,j:longint;begin if (a1b1) and (a2b1) then begin inc(la,2); inc(rb,1); quebrb:=a1+a2; dec(k); sum:=sum+a1+a2; exit; end; if (b1a1) and (b2max then max:=x; end;la:=1;lb:=1;ra:=0;rb:=0;for i:=1 to max do for j:=1 to toti do begin inc(ra); queara:=i; end;k:=n;while k1 do begin a1:=0;a2:=0;b1:=0;b2:=0; if la=ra then a1:=queala else a1:=maxlongint; if la+1=ra then a2:=queala+1 else a2:=maxlongint; if lb=rb then b1:=queblb else b1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论