首页 > 其他 > 详细

NEERC2017 E- Equal Numbers

时间:2018-04-28 18:52:48      阅读:200      评论:0      收藏:0      [点我收藏+]

Equal Numbers

标签:Eratosthenes筛法


题目描述

You are given a list of n integers a1,...,an. You can perform the following operation: choose some ai and multiply it by any positive integer.
Your task is to compute the minimum number of different integers that could be on the list after k operations for all 0≤k≤n.

输入

The first line of the input contains single integer n (1≤n≤3≤105). The second line of the input contains n integers ai (1≤ai≤106).

输出

Output a single line that contains n + 1 integers. The i-th integer should be the minimum possible number of different integers in the list after i-1 operations.

样例输入

6
3 4 1 2 1 2

样例输出

4 4 3 3 2 2 1

分析

  • 贪心的优先修改出现次数较小的元素
  • 对于每个元素可以修改为它在序列中出现的一个倍数,也可以修改为已修改所有数字的\(lcm\),然后取两种方式的最小值

代码

展开

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000500;
int a[maxn],n,M;
int b[maxn],ans[maxn];
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        int x;
        scanf("%d", &x);
        a[x]++;
        M=(x>M?x:M);
    }
    int cnt=0,tot=0;
    for (int i = 1; i <= M; ++i)
    {
        if(!a[i]) continue;
        tot++;
        for (int j = i*2; j <= M; j+=i)
        {
            if(a[j]){
                b[cnt++]=a[i];
                break;
            }
        }
    }
    sort(b,b+cnt);
    memset(ans,0x3f,sizeof ans);
    int p=0,sum=0;
    for (int i = 0; i <= n; ++i)
    {
        while(p<cnt&&sum+b[p]<=i) sum+=b[p],p++;
        ans[i]=min(ans[i],tot-p);
    }
    cnt=0;
    for (int i = 1; i <= M; ++i)
    {
        if(a[i]) b[cnt++]=a[i];
    }
    sort(b,b+cnt);
    p=0,sum=0;
    for (int i = 0; i <= n; ++i)
    {
        while(p<cnt&&sum+b[p]<=i) sum+=b[p],p++;
        ans[i]=min(ans[i],tot-p+1);
        printf("%d%s", ans[i],i<n?" ":"\n");
    }
    return 0;
}

NEERC2017 E- Equal Numbers

原文:https://www.cnblogs.com/sciorz/p/8968954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!