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

本文探讨了在给定两个数组a和b的情况下,通过重新排序数组b来获得字典序最小的数组c的方法。c的每个元素由c[i] = (a[i] + b[i]) % n构成。文章提供了详细的算法思路,包括使用multiset数据结构进行高效查找和更新,以确保最终数组c的字典序最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 

 

 

分析:因为元素的范围是0到n-1,所以对于任何一个a[i]来说,其最小值对于的b[i]应该是第一个大于等于n-a[i]。且要求字典序最小,那么直接从头开始找就行了,没找到的话就用剩余元素中最小的那个。multiset维护即可。

#include<bits/stdc++.h>

using namespace std;
int a[200004];
int b[200004];
int c[200004];

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    multiset<int> s;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &b[i]);
        s.insert(b[i]);
    }
    for (int i = 1; i <= n; ++i) {
        int temp = n - a[i];
        auto pos = s.lower_bound(temp);
        if (pos == s.end()) {
            c[i] = (a[i] + *s.begin());
            s.erase(s.begin());
        } else {
            c[i] = (a[i] + *pos) % n;
            s.erase(pos);
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%d ", c[i]);
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值