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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiang_6/article/details/89739035

思路:

首先找大于等于n的,最小的,没有的话就找最小的

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;

const int maxn = 2e5 + 7;

int n;
int a[maxn];
multiset<int> b;
int ans[maxn];


int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    for(int i = 1; i <= n; ++i) {
        int x; scanf("%d", &x);
        b.insert(x);
    }
    set<int>::iterator t;
    set<int>::iterator t1;
    for(int i = 1; i <= n; ++i) {
        int x = (n-a[i]);
        t = b.lower_bound(x);
        if(t != b.end()) {
            ans[i] = (a[i] + *t) % n;
            b.erase(t);
        }
        else {
            t1 = b.begin();
            ans[i] = (a[i] + *t1) % n;
            b.erase(t1);
        }
    }
    for(int i = 1; i <= n; ++i) {
        if(i > 1)  printf(" ");
        printf("%d", ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiang_6/article/details/89739035