E. Minimum Array(multiset暴力)

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 

题解:给定a数组和b数组,可随意改变b数组的顺序使得c[i]=(a[i]+b[i])%n字典序最小。

用multiset从小到大维护b数组,当a数组为a[i]时,b[i]=(n-a[i])为最优解,当b[i]从(n-a[i])增加到n时:c数组由0变到a[i],当b[i]从0变到n-a[i]-1时,c数组由a[i]变到n-1.

所以我们首先在multiset中二分查找第一个大于等于n-a[i]的数,如果找不到在从multiset中从小到大取值即可~~~

#include<bits/stdc++.h>
using namespace std;
multiset<int>s;
int n,x;
int a[200020];
int main()
{
    std::ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
        cin>>x,s.insert(x);
    multiset<int>::iterator it;
    for(int i=1;i<=n;i++)
    {
        it=s.lower_bound(n-a[i]);
        if(it==s.end())
            it=s.begin();
        cout<<(a[i]+*it)%n<<" ";
        s.erase(it);
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值