主题链接: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. 大容量XFS异常处理

    今天在检查报警的时候发现部分数据异常,登陆备份机查看文件状态的时候发现磁盘空间已经满了,inode也已经有问题了,因此先停掉了所有的备份进程,开始检查问题. 首先是磁盘空间满了,尝试删除部分文件,发现 ...

  2. A - Alignment of Code(推荐)

    You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is ...

  3. 2015 Multi-University Training Contest 1

    最近真是太废柴了,题没做几道,也没学什么新知识,多校做了三场也没总结~诶!好好学吧! 多校第一场感觉被完虐...orz... Hdu 5288 OO’s Sequence 题目链接:http://ac ...

  4. KMP poj

    题目来自:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2315188.html KMP算法开始是判断字符串b是否是字符串a的子串,朴素的算法是枚举 ...

  5. 【deep learning学习笔记】注释yusugomori的LR代码 --- 模型测试

    测试部分代码: void test_lr() { srand(0); double learning_rate = 0.1; double n_epochs = 500; int train_N = ...

  6. 【ASP.NET Web API教程】5.4 ASP.NET Web API批处理器

    原文:[ASP.NET Web API教程]5.4 ASP.NET Web API批处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. ...

  7. 这里的*号实际表示就是RAC中所有实例都使用

    您的位置: ITPUB个人空间 » cc59的个人空间 » 日志 发布新日志 我的日志我的足迹我的收藏 unix/linuxHA随笔backup&restoreperformance tuni ...

  8. python 循环中的else

    众多语言中都有if else这对条件选择组合,但是在python中还有更多else使用的地方,比如说循环for,或者while都可以和else组合. 下面简单介绍一下for-else while-el ...

  9. Ubuntu升级到14.04

    公司网络实在太翔了,搞了一天最终成功把ubuntu从13.10升级到了14.10,中间也越到了非常多问题,记录下来,以备參考. 13.10的时候想体验一把搜狗输入法,结果因为fcitx版本号太低,用了 ...

  10. Disqus – About Disqus

    Disqus – About Disqus   Disqus is a free service that enables great online communities. As the web's ...