0
点赞
收藏
分享

微信扫一扫

Problem - 1176D - Codeforces(贪心+模拟)

_刘彦辉 2022-01-24 阅读 39
算法

Problem - 1176D - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int MAX = 1e6 + 5;
const int N = 5e5 + 5;
int a[N];
bool notPrime[50000005];
int Prime[MAX];
int cnt = 0;
int n;
inline void solve(int n) {
    for (int i = 2; i <= n; ++i) {
        if (!notPrime[i]) {
            Prime[++cnt] = i;
        }
        for (int j = 1; j <= cnt && Prime[j] * i <= n; ++j) {
            notPrime[Prime[j] * i] = true;
            if (i % Prime[j] == 0)break;
        }
    }
}
int num[N];
int main() {
    syncfalse
#ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
#endif
    solve(3000000);
    int n;
    cin >> n;
    n *= 2;
    vector<int>ans;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        num[a[i]]++;
    }
    sort(a + 1, a + 1 + n);
    for (int i = n; i >= 1; --i) {
        if (num[a[i]] == 0)continue;
        num[a[i]]--;
        if (notPrime[a[i]]) {
            for (int j = 1; j <= cnt; ++j) {
                if (a[i] % Prime[j] == 0) {
                    ll tem = a[i] / Prime[j];
                    num[tem]--;
                    ans.push_back(a[i]);
                    break;
                }
            }
        }
        else {
            ll index = lower_bound(Prime + 1, Prime + 1 + cnt, a[i]) - Prime;
            num[index]--;
            ans.push_back(index);
        }
    }
    for (auto x : ans)cout << x << " ";

    return 0;
}
举报

相关推荐

0 条评论