Codeforces Round # 555 (Div. 3) Minimum array (multiset)

题目链接:http://codeforces.com/contest/1157/problem/E

题意:调整b数组顺序,使得c数组最小化(c[i] = a[i] + b[i]) % n,因为这里a[i],b[i] < n,所以我们对每个a[i]在b数组中查找一个>=n - a[i]的值即可满足c数组最小。

因为b数组里面存着重复元素,我们使用multiset这个数据结构来实现查找

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <climits>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
static const int MAX_N = 2e5 + 5;
static const ll Mod = 2009;
int a[MAX_N];
multiset<int>se;
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int n;
    while(scanf("%d", &n) != EOF){
        se.clear();
        for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
        for(int i = 0; i < n; ++i){
            int v;
            scanf("%d", &v);
            se.insert(v);
        }
        for(int i = 0; i < n; ++i){
            int v = (n - a[i]) % n;
            auto it = se.lower_bound(v);    //二分查找>=v的元素地址
            if(it == se.end()) it = se.begin();
            printf("%d%c", (*it + a[i]) % n, i == n - 1 ? '\n' : ' ');
            se.erase(it);
        }
    }
}
int main() {
    solve();
    return 0;
}
View Code

 

posted @ 2019-06-02 13:17  html_11  阅读(133)  评论(0编辑  收藏  举报