[codevs3296]有序数组合并
2015-08-19 11:04
281 查看
题目描述 Description
合并两个有序数组A和B,使得结果依然有序。进阶:合并两个有序数组A和B,假设A有n个数,B有m个数,A数组后面还有m个空余空间,需要将结果保存在A中。
请使用O(n)的算法完成
输入描述 Input Description
第一行输入两个整数n和m第二行输入n个用空格隔开的整数表示数组A
第三行输入m个用空格隔开的整数表示数组B
输入保证A和B数组非递减
输出描述 Output Description
输出一行一共n+m个空格隔开的整数,即合并后的结果样例输入 Sample Input
2 31 2
1 1 5
样例输出 Sample Output
1 1 1 2 5数据范围及提示 Data Size & Hint
1<=n,m<=1000000思路
用三个指针分别指向正在处理的两个数组和结果数组,最后需要看一看是不是全部比较完了,时间复杂度O(n);var n,m,i,j,k,x:longint; a,b,c:array[1..10000000] of int64; begin readln(n,m); for i:=1 to n do read(a[i]); for i:=1 to m do read(b[i]); i:=1;//a的指针 j:=1;//b的指针 k:=1;//末尾的指针 while (i<>n+1)and(j<>m+1) do begin if a[i]<=b[j] then begin c[k]:=a[i]; inc(i); inc(k); end else begin c[k]:=b[j]; inc(j); inc(k); end; end; // inc(k); for x:=i to n do begin c[k]:=a[x]; inc(k); end; for x:=j to m do begin c[k]:=b[x]; inc(k); end; for i:=1 to (n+m) do write(c[i],' '); end.
View Code
相关文章推荐
- Codeforces Gym100650C The Game of Efil
- 定位服务2
- Resizing array笨办法,每次都要新建一个临时数组
- android使用PopupWindow实现页面点击顶部弹出下拉菜单
- Spring整合JMS——基于ActiveMQ实现
- java list三种遍历方法性能比较
- 专访罗升阳:老罗的Android之旅
- POJ1988 Cube Stacking
- CountDownTimer官方介绍和使用
- 长连接
- 快捷键整理
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法【转载】
- Redis安装与日常使用
- hadoop native
- 使用ffmpeg 对视频截图,和视频转换格式
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- [Android]RecyclerView的简单示例
- 数组笔记
- druid简单教程
- Oracle SQL性能优化