CodeForces 1157E 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

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 

思路:根据题意很容易想到贪心,让前面的ci最小,但是在搜索bi时如果采用遍历查找,则复杂度为O(n^2)根据题目数据量会TLE,所以此处需要用两边二分查找,先找后面,再找前面。我在此题之前没接触过multiset容器,所以采用了树状数组来作为存数容器进行二分查找。

AC代码1 树状数组:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 2e5 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
ll n, m;
int arra[maxn];
int arrb[maxn];
int re[maxn];
int lowbit(int x)
{
    return (-x)&x;
}
void add(int x, int y){
    while(x <= n){
        re[x]+=y;
        x+=lowbit(x);
    }
}
int getsum(int x){
    int sum = 0;
    while(x)
    {
        sum += re[x];
        x -= lowbit(x);
    }
    return sum;
}

int main()
{
	cin >> n;
	int ta;
	rep(i, 1, n){
        cin >> arra[i];
	}
	rep(i, 1, n){
        cin >> ta;
        arrb[ta+1]++;
	}
	rep(i, 1, n){
        add(i, arrb[i]);
	}
	rep(i, 1, n){
	    if(i!=1)
            cout << ' ';
        int ans = -1;
        int l = n-arra[i]+1, r = n;
        while(l <= r){
            int mid = (l+r)/2;
            if(getsum(mid) - getsum(l-1))
            {
                ans = mid;
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        if(ans != -1){
            cout << (ans+arra[i]-1)%n;
            add(ans, -1);
        }
        else{
            l = 1, r = n-arra[i];
            while(l <= r){
                int mid = (l+r)/2;
                if(getsum(mid) - getsum(l-1))
                {
                    ans = mid;
                    r = mid-1;
                }else{
                    l = mid+1;
                }
            }
            cout << (ans+arra[i]-1)%n;
            add(ans, -1);
        }

	}
	cout << endl;
	return 0;
}

AC代码2: multiset

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 2e5 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
ll n, m;
int arra[maxn];
int arrb[maxn];
multiset<int> mst;


int main()
{
	cin >> n;
	int ta;
	rep(i, 1, n){
        cin >> arra[i];
	}
	rep(i, 1, n){
        cin >> ta;
        mst.insert(ta);
	}
	multiset<int>::iterator it;
	rep(i, 1, n){
	    if(i != 1)
            cout << " ";
        it = mst.lower_bound(n-arra[i]);
        if(it == mst.end()) it = mst.begin();
        cout << (arra[i]+*it)%n;
        mst.erase(it);
	}
	cout <<endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值