感觉我双指针不行啊...写了个multiset过的。看了别人代码改了发双指针。
写几个样例大概就知道跟田忌赛马一样了。
#include <bits/stdc++.h> const int N = 1e5 + 7; int a[N], b[N]; std::multiset<int> st1, st2; bool vis[N]; int solve(int A[], int B[], int n) { int ans = 0; int l1 = 1, l2 = 1, r1 = n, r2 = n; while (l1 <= r1 && l2 <= r2) { if (A[l1] > B[l2]) ans += 2, l1++, l2++; else if (A[r1] > B[r2]) ans += 2, r1--, r2--; else ans += (A[l1] == B[r2]) ? 1 : 0, l1++, r2--; } return ans; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", b + i); std::sort(a + 1, a + 1 + n); std::sort(b + 1, b + 1 + n); printf("%d %d\n", solve(a, b, n), 2 * n - solve(b, a, n)); }View Code