题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列。

  插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同。

  堆排序的特点就是,后面是从小到大排列的最大的几个数p~n-1,前面第一位则是p-1。

  所以只要先按照插入排序的特点来判断是否为插入排序,接下来再进行操作就可以了,这里要手动写下最大堆的更新操作。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <string.h>
  5. /*
  6.  
  7. 之前一直WA的原因是,用for循环写寻找idx一开始就写错了。。。
  8. 找了整个序列的<,应该是找反例>从而跳出for循环,或者直接加到条件里。
  9. 比如:
  10. 一开始这么写,
  11. for(int i=0;i<n;i++){
  12. if(num2[i]<=num2[i+1])
  13. idx++;
  14. }
  15. 正确应该是:
  16. for(idx=0;idx<n-1 && num2[idx]<=num2[idx+1];idx++);
  17. 晕死。。。脑子糊涂了
  18. */
  19. using namespace std;
  20. const int maxn=;
  21. int heap[maxn];
  22. int heap_size=;
  23.  
  24. void swaps(int &a,int &b){
  25. int tmp;
  26. tmp=a;
  27. a=b;
  28. b=tmp;
  29. }
  30. void heap_update(int i){
  31. int bigger=i;
  32. int l=(i<<)+;
  33. int r=(i<<)+;
  34. if(l<heap_size && heap[l]>heap[i])
  35. bigger=l;
  36. if(r<heap_size && heap[r]>heap[bigger])
  37. bigger=r;
  38. if(bigger!=i){
  39. swaps(heap[i],heap[bigger]);
  40. heap_update(bigger);
  41. }
  42. }
  43. void heap_pop(){
  44. if(heap_size>=){
  45. swaps(heap[],heap[heap_size-]); //take the max to the rear.
  46. heap_size--;
  47. heap_update();
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. int n;
  54. int num[maxn],num2[maxn];
  55.  
  56. scanf("%d",&n);
  57. for(int i=;i<n;i++){
  58. scanf("%d",&num[i]);
  59. }
  60. for(int i=;i<n;i++){
  61. scanf("%d",&num2[i]);
  62. }
  63. int idx;
  64. for(idx=;idx<n- && num2[idx]<=num2[idx+];idx++);
  65. int p=idx+;
  66. for(;p<n && num[p]==num2[p];p++);
  67. if(p==n){
  68. sort(num2,num2+idx+); //相当于将num[idx+1]插入到前面排序
  69. printf("Insertion Sort\n");
  70. for(int i=;i<n-;i++)
  71. printf("%d ",num2[i]);
  72. printf("%d",num2[n-]);
  73. }
  74. else{
  75. int sortedsize=;
  76. int tmpnum[maxn];
  77. for(int i=;i<n;i++)
  78. tmpnum[i]=num[i];
  79. sort(tmpnum,tmpnum+n);
  80. p=n-;
  81. /*
  82. 看了别人网上有写第三行AC的,
  83. 但按道理来说,如果样例2的第二个序列是6 4 5 0 1 2 3 7 8 9,那明显第三行就不对额。
  84.  
  85. 10
  86. 3 1 2 8 7 5 9 4 6 0
  87. 6 4 5 0 1 2 3 7 8 9
  88. Heap Sort
  89. 5 4 3 0 1 2 6 7 8 9 (第一行的输出)
  90. 5 4 0 6 1 2 3 7 8 9 (第三行的输出)
  91. */
  92. for(;p>= && num2[p]==tmpnum[p];p--);
  93. //for(;p>=1 && num2[p]>=num2[0];p--);
  94. //for(;p>=1 && num2[p]>=num2[p-1];p--); //个人认为应该是过不了的。。。但却AC了
  95.  
  96. heap_size=p+;
  97. for(int i=;i<n;i++)
  98. heap[i]=num2[i];
  99. heap_pop();
  100. printf("Heap Sort\n");
  101. for(int i=;i<n-;i++){
  102. printf("%d ",heap[i]);
  103. }
  104. printf("%d",heap[n-]);
  105. }
  106. return ;
  107. }

PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)的更多相关文章

  1. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  2. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  4. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  5. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  6. 1098. Insertion or Heap Sort (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. 1098 Insertion or Heap Sort (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. PAT甲题题解-1002. A+B for Polynomials (25)-多项式相加

    注意两点:1.系数也有可能加起来为负!!!一开始我if里面判断为>0导致有样例没过...2.如果最后所有指数的系数都为0,输出一个0即可,原本以为是输出 1 0 0.0... #include ...

  9. PAT甲题题解-1039. Course List for Student (25)-建立映射+vector

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789157.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. win7 win10双系统开机系统引导

    以win7启动管理器引导作为启动引导 安装一个easybcd 然后里面添加引导选项(添加新条目---->编辑引导菜单(选择倒计时30秒)把use metro bootloader勾去掉就是默认的 ...

  2. 【Alpha 冲刺】 7/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 建立数据库 已完成 孙浩楷 完成作业列表界面 已完成 胡冰 完成作业展示页面 已完成 练斐弘 完成课件列表页面 未完成 时间不够 ...

  3. 如何安装Firebug

    Firebug是firefox下的一个插件,能够调试所有网站语言,如Html,Css等,但FireBug最吸引我的就是javascript调试功能,使用起来非常方便,而且在各种浏览器下都能使用(IE, ...

  4. ArcGIS API for Javascript之专题图的制作(四)热力图渲染(上)

    一 .热力图定义 热力图(heat map)也称热图,是以特殊颜色高亮区域的形式表示密度.温度.气压.频率等分布的不易理解和表达的数据. 二.HeatmapRenderer esri/renderer ...

  5. Oracle Spatial中SDO_Geometry说明

    Oracle Spatial中SDO_Geometry说明 在ArcGIS中通过SDE存储空间数据到Oracle中有多种存储方式,分别有:二进制Long Raw .ESRI的ST_Geometry以及 ...

  6. bat替换文件的指定内容

    需求:替换文件my.ini中的1000 为10000,bat脚本如下: c:cd C:\Program Files\MySQL\MySQL Server 5.5copy my.ini my1126ba ...

  7. POJ2104 K-th Number(整体二分)

    嘟嘟嘟 整体二分是一个好东西. 理解起来还行. 首先,需要牢记的是,我们二分的是答案,也就是在值域上二分,同时把操作分到左右区间中(所以操作不是均分的). 然后我就懒得讲了-- 李煜东的<算法竞 ...

  8. Eclipse Mars 2安装Drools6.4插件(Drools and jBPM tools)时无法安装JBoss Runtime Drools Detector

    在eclipse上本地安装Drools6.4Final的时候出现两个组件无法正常安装的情况,具体组件如下: 具体的提示信息为: Cannot complete the install because ...

  9. mysql中的delete , drop 和truncate 区别

    1.delete 和 truncate 仅仅删除表数据,drop 连表数据和表结构一起删除,打个比方,delete 是单杀,truncate 是团灭,drop 是把电脑摔了. 2.delete 是 D ...

  10. 节点的启动与关闭 ros::init()解析(c++)

    1.初始化roscpp 节点 ros::init()  API链接:http://docs.ros.org/api/roscpp/html/init_8h.html 在node代码中在调用其它rosc ...