Codeforces Round #555 (Div. 3) E. Minimum Array

 

E. Minimum Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two arrays aa and bb, both of length nn. All elements of both arrays are from 00 to n−1n−1.

You can reorder elements of the array bb (if you want, you may leave the order of elements as it is). After that, let array cc be the array of length nn, the ii-th element of this array is ci=(ai+bi)%nci=(ai+bi)%n, where x%yx%y is xx modulo yy.

Your task is to reorder elements of the array bb to obtain the lexicographically minimum possible array cc.

Array xx of length nn is lexicographically less than array yy of length nn, if there exists such ii (1≤i≤n1≤i≤n), that xi<yixi<yi, and for any jj (1≤j<i1≤j<i) xj=yjxj=yj.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa, bb and cc.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai<n0≤ai<n), where aiai is the ii-th element of aa.

The third line of the input contains nn integers b1,b2,…,bnb1,b2,…,bn (0≤bi<n0≤bi<n), where bibi is the ii-th element of bb.

Output

Print the lexicographically minimum possible array cc. Recall that your task is to reorder elements of the array bb and obtain the lexicographically minimum possible array cc, where the ii-th element of cc is ci=(ai+bi)%nci=(ai+bi)%n.

Examples

input

Copy

4
0 1 2 1
3 2 1 1

output

Copy

1 0 0 2 

input

Copy

7
2 5 1 5 3 4 3
2 4 3 5 6 5 1

output

Copy

0 0 0 1 0 2 4 

 

 

这道题目解法的思路很简单,就是获得的c_i 要从左到右计算,要尽量获得小的数值。因此,要从B中取得数字b_i使得 (a_i + b_i) %n 尽量小。 所有的b_i 需要存放在multiset中,因为multiset可以根据数值大小进行二分查找,同时又可以删除元素,并且两个操作都是logn复杂度,使得总复杂度为nlogn。

另外, multiset要只删除一个元素,需要使用这个元素的迭代器。如果直接删除值,会把所有相同的值都删除掉。

#include <bits/stdc++.h>

#define REP(i, s, k) for(int i = s; i < k; i++)
using namespace std;
using ll = long long;

const int INF = 2e9;
const int MAXN = 2e5+10;

vector<int> A;
multiset<int> B;
signed main(){
  int n; int t;
  scanf("%d", &n);
  REP(i, 0, n) {scanf("%d", &t); A.emplace_back(t);}
  REP(i, 0, n) {scanf("%d", &t); B.insert(t);}
  REP(i, 0, n){
    multiset<int>::iterator it;
    int tmp = (A[i] + *B.begin() ) % n;
    it = B.lower_bound(n - A[i]);
    if(it == B.end() || (*it + A[i]) % n > tmp ) it = B.begin();
    printf("%d ", (*it + A[i]) % n);
    B.erase(it);
  }
  return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值