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.
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.
For each test case you should output the median of the two given sequences in a line.
- 4 11 12 13 14
- 5 9 10 15 16 17
13
总结:这题按道理来说应该也是很简单的,但是不知道哪里出错了,还有好几个测试点没有通过(18分),以后有机会慢慢研究一下
- //将两个序列合并(按照归并排序的方式)
- #include
- using namespace std;
-
- const int N=1000010;
- int a[N],b[N],c[N];
- int n,m;
-
- int main(){
- cin >> n;
- for(int i=0;i
scanf("%d",&a[i]); - cin >> m;
- for(int i=0;i
scanf("%d",&b[i]); -
- int i=0,j=0,k=0;
- while(i
- if(a[i]<=b[j]) c[k++]=a[i++];
- else c[k++]=b[j++];
- }
-
- while(i
- while(j
-
- printf("%d\n",c[n+m>>1]);
-
- return 0;
- }
代码:
为什么这个代码i,j是从0开始的就错了呢,原因还得从题目样例中出发,s1={11,12,13,14},题目中给出的中位数是12,所以当总个数的是偶数的时候,往左边一个取中位数,所以从0开始算出来的target=4/2=2,指向的是第三个,所以错了(最后还有一个测试点未通过!!!)
- #include
- using namespace std;
-
- const int N=200010;
- int a[N],b[N];
- int n,m;
-
- int main(){
- cin >> n;
- for(int i=0;i
scanf("%d",&a[i]); - cin >> m;
- for(int i=0;i
scanf("%d",&b[i]); -
- //如果是从0开始的话,那么需要-1,才能找到正确的target的位置(此时还有一个测试点没有通过)
- int target=(m+n-1)>>1,i=0,j=0,cot=0,ans=0;
- while(i
- ans=a[i]<=b[j]?a[i++]:b[j++];
- if(cot++==target) break;
- }
-
- if(i
//i - ans=a[i+target-cot];
- }
- else if(j
- ans=b[j+target-cot];
- }
- //如果是cot==target结束循环的,说明已经找到了中点
- printf("%d\n",ans);
- return 0;
- }
-
-
- //正确代码:
- #include
- using namespace std;
-
- const int N=200010;
- int a[N],b[N];
- int n,m;
-
- int main(){
- cin >> n;
- for(int i=1;i<=n;i++) scanf("%d",&a[i]);
- cin >> m;
- for(int i=1;i<=m;i++) scanf("%d",&b[i]);
-
- int target,i,j,cot,ans;
- target=m+n+1>>1,i=1,j=1,cot=0;
- while(i<=n && j<=m){
- ans=a[i]<=b[j]?a[i++]:b[j++];
- if(++cot==target) break;
- }
-
- //下面下标的计算方法:
- //举例:3 4 5
- // 6 7 8 9 10 11 12 此时 target 的值应该为 5
- // 循环结束后,i=n+1,j=0, 如果直接取 b[j] 的话,不是目标结果
- // j+(target-cot-1)的意义是 当前 j 加上离目标值的距离 target-cot-1 表示已经筛了多少个数字
- if(i<=n && cot
- ans=a[i+target-cot-1];
- }
- else if(j<=m && cot
- ans=b[j+target-cot-1];
- }
- printf("%d\n",ans);
- return 0;
- }
好好学习,天天向上!
我要考研!
-
相关阅读:
软件测试的价值
Linux之软件包管理
Java-覆写和重载区别
494.力扣每日一题6/30 Java(三种解法)
Parallels 将扩展桌面平台产品,以进一步改善在 Mac 上运行 Windows 的用户体验和工作效率
20个最佳实践提升Terraform工作流程|Part 2
城市道路拥堵终结者,智能停车场系统组网解决方案
数据库优化 | 干货
外文文献查找技巧方法有哪些
前端TypeScript学习-交叉类型与泛型
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/126876075
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU