题意:给出两串数,第一串数不变,问第二穿数应该怎么顺序才能使输出的(a[i]+b[i])%n的顺序最小。a[i]<n&&b[i]<n
思路:由于a[i]<n&&b[i]<n,则a[i]+b[i]<2*n,则最好是a[i]+b[i]==n。所以二分查找可以ac,复杂度nlogn,然后为了方便用multiset来进行二分,stl真好用。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
int A[N];
int main() {
multiset<int> S;
int n, x;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> A[i];
for (int i = 1; i <= n; ++i) {
cin >> x;
S.insert(x);
}
for (int i = 1; i <= n; ++i) {
auto it = S.lower_bound(n - A[i]);
auto it2 = S.lower_bound(0);
if (it == S.end()) {
printf("%d ", (A[i] + *it2) % n);
S.erase(it2);
} else {
printf("%d ", (A[i] + *it) % n);
S.erase(it);
}
}
return 0;
}