主题链接:http://pat.zju.edu.cn/contests/ds/2-13

已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0, A1…AN-1的中位数指A(N-1)/2的值,即第[(N+1)/2]个数(A0为第1个数)。

输入格式说明:

输入分3行。第1行给出序列的公共长度N(0<N<=100000),随后每行输入一个序列的信息。即N个非降序排列的整数。数字用空格间隔。

输出格式说明:

在一行中输出两个输入序列的并集序列的中位数。

例子输入与输出:

序号 输入 输出
1
5
1 3 5 7 9
2 3 4 5 6
4
2
6
-100 -10 1 1 1 1
-50 0 2 3 4 5
1
3
3
1 2 3
4 5 6
3
4
3
4 5 6
1 2 3
3
5
1
2
1
1

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjg2MDA2Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

PS:

这题说多了都是泪,怪自己智商捉急,看见题目里的并集。就煞笔的去了重,然后……然后最后一个案例WA到吐都过不了!用了各种方法…………

代码一例如以下(链表):

#include <cstdio>
#include <cstring>
#include <malloc.h>
#define LEN sizeof(struct node)
struct node
{
int x;
struct node *next;
};
int N;
struct node *creat()
{
struct node *p1, *p2, *head1;
p1=p2=head1=(struct node*)malloc(LEN);
int t = N;
head1 = NULL;
int n = 0;
while(t--)
{
p1 = (struct node *)malloc(LEN);
scanf("%d",&p1->x);
n++;
if(n == 1)
head1 = p1;
else
p2->next = p1;
p2 = p1; }
p2->next = NULL;
return head1;
} struct node *findd(struct node *p1, struct node *p2)
{
struct node *p3;
p3 = NULL;
if(p1 == NULL)
return p2;
if(p2 == NULL)
return p1;
if(p1->x < p2->x)
{
p3 = p1;
p3->next = findd(p1->next,p2);
}
else if(p1->x == p2->x)
{
p3 = p1;
p3->next = findd(p1->next,p2);
}
else
{
p3 = p2;
p3->next = findd(p1,p2->next);
}
return p3;
}
void print(struct node *head)
{
struct node *p;
p = head;
int n = 0;
while(p!=NULL)
{
if(n == (2*N-1)/2)
{
printf("%d\n",p->x);
break;
}
p = p->next;
n++;
}
}
int main()
{
scanf("%d",&N);
struct node *p, *head1, *head2;
head1 = creat();
head2 = creat();
p = findd(head1, head2);
print(p);
return 0;
}

代码二例如以下(数组):

#include <cstdio>
#include <cstring>
const int maxn = 100017;
int a[maxn], b[maxn], c[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
for(int i = 0; i < n; i++)
{
scanf("%d",&b[i]);
}
int l = 0;
int i = 0, j = 0;
while(i < n && j < n)
{
if(a[i] == b[j])
{
c[l++] = a[i];
c[l++] = b[j];//这一行開始没加WA到吐
i++,j++;
}
if(a[i] < b[j])
{
c[l++] = a[i];
i++;
}
else
{
c[l++] = b[j];
j++;
}
}
while(i < n)
{
c[l++] = a[i];
i++;
}
while(j < n)
{
c[l++] = b[j];
j++;
}
printf("%d\n",c[(l-1)/2]);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

2-13. 平均两个有序序列(25)(ZJU_PAT 名单 | 排列 )的更多相关文章

  1. PAT 2-13. 两个有序序列的中位数(25)

    题目链接:http://www.patest.cn/contests/ds/2-13 解题思路及代码如下: /* 解题思路: 分别求出序列A 和B 的中位数,设为a 和b,求序列A 和B 的中位数过程 ...

  2. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  3. 求两个有序序列合并成新有序序列的中位数,求第k小数

    此算法涉及一个重要数学结论:如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明. 算法思想如下: 1,假设A长度为m,B长度为n, ...

  4. merge two sorted lists, 合并两个有序序列

    /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...

  5. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. 求两个有序数组的中位数(4. Median of Two Sorted Arrays)

    先吐槽一下,我好气啊,想了很久硬是没有做出来,题目要求的时间复杂度为O(log(m+n)),我猜到了要用二分法,但是没有想到点子上去.然后上网搜了一下答案,感觉好有罪恶感. 题目原型 正确的思路是:把 ...

  7. PTA 两个有序链表序列的合并

    6-5 两个有序链表序列的合并 (15 分)   本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L1, List L ...

  8. 02-线性结构1 两个有序链表序列的合并(15 point(s)) 【链表合并】

    02-线性结构1 两个有序链表序列的合并(15 point(s)) 本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L ...

  9. UVALive 7077 Little Zu Chongzhi's Triangles (有序序列和三角形的关系)

    这个题--我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了--结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小, ...

随机推荐

  1. Appium Server 传递Android参数

    Appium  server Capabilities 传递参数    Android 特定 Android Only Capability Description Values appActivit ...

  2. shell oracle

    #!/bin/sh traffic= rm -rf test.txt data=`sqlplus -S anoscfg/anoscfg <<EOF spool test.txt set f ...

  3. Skype无法显示登录界面

    Skype升级之后突然抽风,双击运行程序之后,输入用户名和密码的窗口都没了,截图如下(本机为Windows 7 32bit版本): 卸载重新安装,也无济于事.删除注册表中的Skype的相关信息后问题依 ...

  4. hdu1869 六度分离(Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 转载请注明出处:http://blog.csdn.net/u012860063?viewmode ...

  5. Codeforces 39E What Has Dirichlet Got to Do with That? 游戏+内存搜索

    主题链接:点击打开链接 意甲冠军: 特定 a一箱 b球 不变n (球和箱子都不尽相同,样的物品) 设 way = 把b个球放到a个箱子中的方法数, 若way >= n则游戏结束 有2个人玩游戏. ...

  6. HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  7. STM8S 串口应用 UART2 STM8S105

    //少说话.多做事,下面是我验证过没有问题的串口发送接受数据 //使用MCU stm8s105c6 UART2 //初始化时调用: GPIO_DeInit(GPIOD); /* Configure P ...

  8. ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件

    本篇讲解三个容器类控件. 一.面板控件 Ext.Panel 一个面板控件包括几个部分,有标题栏.工具栏.正文.按钮区.标题栏位于最上面,工具栏可以在四个位置放置,围绕中间部分正文,按钮区位于最小方.下 ...

  9. centos 安装 redis3.2.0 集群

    这里创建6个redis节点,其中三个为主节点,三个为从节点. redis和端口对应关系: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 从: 127.0.0 ...

  10. NetBSD是个开源到源码的系统

    How to get NetBSD NetBSD is an Open Source operating system, and as such it is freely available for ...