2019.10.22-魔法(堆)

和自甴很熟 提交于 2019-12-01 22:45:18

题目大意:给定两个\(n\)元组,求其中不同组元素相加得的结果最小前\(n\)
排掉,加上,扔堆,拿出,回去,没了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else

#define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ;

#endif

struct ios {
    template<typename ATP> ios& operator >> (ATP &x) {
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x *= f;
        return *this;
    }
}io;

using namespace std;

template<typename ATP> inline ATP Min(ATP a, ATP b) {
    return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
    return a > b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
    return a < 0 ? -a : a;
}

const int N = 500007;

int a[N], b[N];
struct nod{
    int x, y;
    bool operator < (const nod &com) const {
        return a[x] + b[y] > a[com.x] + b[com.y];
    }
};
#include <queue>
priority_queue<nod> q;
int main() {
freopen("magic.in", "r", stdin);
freopen("magic.out", "w", stdout);
    int n;
    io >> n;
    R(i,1,n) io >> a[i];
    R(i,1,n) io >> b[i];
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    R(i,1,n){
        q.push((nod){ i, 1});
    }
    R(i,1,n){
        nod u = q.top();
        q.pop();
        printf("%d\n", a[u.x] + b[u.y]);
        ++u.y;
        if(u.y < n)
            q.push(u);
    }
    
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!