• PAT 1029 Median


    1029 Median

    Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

    Given two increasing sequences of integers, you are asked to find their median.

    Input Specification:

    Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤2×105) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

    Output Specification:

    For each test case you should output the median of the two given sequences in a line.

    Sample Input:

    1. 4 11 12 13 14
    2. 5 9 10 15 16 17

    Sample Output:

    13

    总结:这题按道理来说应该也是很简单的,但是不知道哪里出错了,还有好几个测试点没有通过(18分),以后有机会慢慢研究一下

    1. //将两个序列合并(按照归并排序的方式)
    2. #include
    3. using namespace std;
    4. const int N=1000010;
    5. int a[N],b[N],c[N];
    6. int n,m;
    7. int main(){
    8. cin >> n;
    9. for(int i=0;iscanf("%d",&a[i]);
    10. cin >> m;
    11. for(int i=0;iscanf("%d",&b[i]);
    12. int i=0,j=0,k=0;
    13. while(i
    14. if(a[i]<=b[j]) c[k++]=a[i++];
    15. else c[k++]=b[j++];
    16. }
    17. while(i
    18. while(j
    19. printf("%d\n",c[n+m>>1]);
    20. return 0;
    21. }

     代码:

    为什么这个代码i,j是从0开始的就错了呢,原因还得从题目样例中出发,s1={11,12,13,14},题目中给出的中位数是12,所以当总个数的是偶数的时候,往左边一个取中位数,所以从0开始算出来的target=4/2=2,指向的是第三个,所以错了(最后还有一个测试点未通过!!!)

    1. #include
    2. using namespace std;
    3. const int N=200010;
    4. int a[N],b[N];
    5. int n,m;
    6. int main(){
    7. cin >> n;
    8. for(int i=0;iscanf("%d",&a[i]);
    9. cin >> m;
    10. for(int i=0;iscanf("%d",&b[i]);
    11. //如果是从0开始的话,那么需要-1,才能找到正确的target的位置(此时还有一个测试点没有通过)
    12. int target=(m+n-1)>>1,i=0,j=0,cot=0,ans=0;
    13. while(i
    14. ans=a[i]<=b[j]?a[i++]:b[j++];
    15. if(cot++==target) break;
    16. }
    17. if(i//i
    18. ans=a[i+target-cot];
    19. }
    20. else if(j
    21. ans=b[j+target-cot];
    22. }
    23. //如果是cot==target结束循环的,说明已经找到了中点
    24. printf("%d\n",ans);
    25. return 0;
    26. }
    27. //正确代码:
    28. #include
    29. using namespace std;
    30. const int N=200010;
    31. int a[N],b[N];
    32. int n,m;
    33. int main(){
    34. cin >> n;
    35. for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    36. cin >> m;
    37. for(int i=1;i<=m;i++) scanf("%d",&b[i]);
    38. int target,i,j,cot,ans;
    39. target=m+n+1>>1,i=1,j=1,cot=0;
    40. while(i<=n && j<=m){
    41. ans=a[i]<=b[j]?a[i++]:b[j++];
    42. if(++cot==target) break;
    43. }
    44. //下面下标的计算方法:
    45. //举例:3 4 5
    46. // 6 7 8 9 10 11 12 此时 target 的值应该为 5
    47. // 循环结束后,i=n+1,j=0, 如果直接取 b[j] 的话,不是目标结果
    48. // j+(target-cot-1)的意义是 当前 j 加上离目标值的距离 target-cot-1 表示已经筛了多少个数字
    49. if(i<=n && cot
    50. ans=a[i+target-cot-1];
    51. }
    52. else if(j<=m && cot
    53. ans=b[j+target-cot-1];
    54. }
    55. printf("%d\n",ans);
    56. return 0;
    57. }

    好好学习,天天向上!

    我要考研!

  • 相关阅读:
    渗透测试-红队从资产收集到打点
    个人主页汇总 | 私信没空看,建议b站
    链表经典题带刷(内含精华:链表深拷贝)
    MARKLLM——LLM 水印开源工具包
    无线传感器网络:物理层设计
    python根据多边形polygon生成掩膜图像问题cv2.fillPoly()和cv2.fillConvexPoly()
    Linux ping向网络主机发送ICMP ECHO REQUEST
    1024程序员节特辑 | ELK+ 用户画像构建个性化推荐引擎,智能实现“千人千面”
    随笔 | 写在九月末的这一天
    功率放大器的类型和特点是什么(功率放大器使用注意事项有哪些)
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/126876075