forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAC_simulation_n.cpp
48 lines (43 loc) · 1.04 KB
/
AC_simulation_n.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2014-12-18 10:37:41
* Descripton: simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
// move A[0~m-1] to A[n~n+m-1]
for (int i = m - 1; i >= 0; i--)
A[n + i] = A[i];
// merge
int pa = n, pb = 0, cnt = 0;
while (pa < n + m && pb < n) {
if (A[pa] > B[pb])
A[cnt++] = B[pb++];
else
A[cnt++] = A[pa++];
}
while (pb < n)
A[cnt++] = B[pb++];
}
};
int main() {
int a[100], b[100];
int n, m;
Solution s;
while (cin >> n >> m) {
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < m; i++)
scanf("%d", &b[i]);
s.merge(a, n, b, m);
for (int i = 0; i < n + m; i++)
printf("%d ", a[i]);
puts("");
}
return 0;
}