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

4 篇文章 0 订阅

题目链接

You are given two arrays a and b, both of length n. All elements of both arrays are from 0 to n−1.

You can reorder elements of the array b (if you want, you may leave the order of elements as it is). After that, let array c be the array of length n, the i-th element of this array is ci=(ai+bi)%n, where x%y is x modulo y.

Your task is to reorder elements of the array b to obtain the lexicographically minimum possible array c.

Array x of length n is lexicographically less than array y of length n, if there exists such i (1≤i≤n), that xi<yi, and for any j (1≤j<i) xj=yj.

Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in a, b and c.

The second line of the input contains n integers a1,a2,…,an (0≤ai<n), where ai is the i-th element of a.

The third line of the input contains n integers b1,b2,…,bn (0≤bi<n), where bi is the i-th element of b.

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

Examples
inputCopy
4
0 1 2 1
3 2 1 1
outputCopy
1 0 0 2
inputCopy
7
2 5 1 5 3 4 3
2 4 3 5 6 5 1
outputCopy
0 0 0 1 0 2 4

题意: 两个长度为n的数组,数组a位置不变,改变数组b的位置,构造数组c ci=(ai+bi)%n,使得数组c字典序最小。
思路: 使得数组c的字典序最小,即每次bi使得ai+bi大于n并且最接近n,因此用multiset维护数组b,lower_bound(n-a[i])即可。

#include<bits/stdc++.h>
using namespace std;
#define sc(x)   scanf("%d",&x)
int a[210000],b[210000];
multiset<int>mu;
int main()
{
    int n;
    sc(n);
    for(int i=0;i<n;i++)    sc(a[i]);
    for(int i=0;i<n;i++){int x;sc(x);mu.insert(x);};
    multiset<int>::iterator it;
    for(int i=0;i<n;i++){
        it = mu.lower_bound(n-a[i]);
        if(it == mu.end())  it = mu.begin();
        cout<<(a[i]+*it)%n<<" ";
        mu.erase(it);
    }
    cout<<endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值