描述
题解
这个题如果全部用 set 写,那么就很简单了,如果用数组写,那就是一个细心问题,别粗心害死自己。
我用的方法效率挺可观的,竟然超时了,后来发现,我开的数组不够大,因为每个数组都是不超过 1000 个元素,那么并起来就是不超过两千个,我只开了一千余个肯定不够用~~~
代码
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int MAXN = 2222;
const int INF = 0x7fffffff;
int A[MAXN], B[MAXN];
int C[MAXN], D[MAXN];
set<int> E;
int main(int argc, const char * argv[])
{
// freopen("/Users/zyj/Desktop/input.txt", "r", stdin);
int n, m;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> B[i];
}
sort(A, A + n);
sort(B, B + m);
A[n] = B[m] = INF;
int i = 0, j = 0;
int cntC = 0, cntD = 0;
if (A[0] < B[0])
{
while (i < n)
{
D[cntD++] = A[i];
while (j < m && B[j] < A[i + 1])
{
if (B[j] != A[i])
{
D[cntD++] = B[j];
}
else
{
C[cntC++] = B[j];
}
j++;
}
i++;
}
while (j < m)
{
D[cntD++] = B[j];
j++;
}
}
else
{
while (j < m)
{
D[cntD++] = B[j];
while (i < n && A[i] < B[j + 1])
{
if (B[j] != A[i])
{
D[cntD++] = A[i];
}
else
{
C[cntC++] = A[i];
}
i++;
}
j++;
}
while (i < n)
{
D[cntD++] = A[i];
i++;
}
}
for (int i = 0; i < cntC; i++)
{
cout << C[i] << ' ';
E.insert(C[i]);
}
cout << '\n';
for (int i = 0; i < cntD; i++)
{
cout << D[i] << ' ';
}
cout << '\n';
for (int i = 0; i < n; i++)
{
if (!E.count(A[i]))
{
cout << A[i] << ' ';
}
}
cout << '\n';
return 0;
}