zoukankan      html  css  js  c++  java
  • CF1209A Paint the Numbers

    You are given a sequence of integers a1,a2,,an. You need to paint elements in colors, so that:

    • If we consider any color, all elements of this color must be divisible by the minimal element of this color.
    • The number of used colors must be minimized.

    For example, it's fine to paint elements [40,10,60]in a single color, because they are all divisible by 10. You can use any color an arbitrary amount of times (in particular, it is allowed to use a color only once). The elements painted in one color do not need to be consecutive.

    For example, if a=[6,2,3,4,12]then two colors are required: let's paint 63 and 12 in the first color (63 and 12 are divisible by 3) and paint 2 and 4 in the second color (2 and 4 are divisible by 2). For example, if a=[10,7,15]then 3 colors are required (we can simply paint each element in an unique color).

    Input

    The first line contains an integer n (1n100), where n is the length of the given sequence.

    The second line contains n integers a1,a2,,an (1ai100). These numbers can contain duplicates.

    Output

    Print the minimal number of colors to paint all the given numbers in a valid way.

    Examples
    input
    6
    10 2 3 5 4 2
    
    output
    3
    
    input
    4
    100 100 100 100
    
    output
    1
    
    input
    8
    7 6 5 4 3 2 2 3
    
    output
    4
    
    Note

    In the first example, one possible way to paint the elements in 3 colors is:

    • paint in the first color the elements: a1=10 and a4=5,
    • paint in the second color the element a3=3,
    • paint in the third color the elements: a2=2a5=4 and a6=2.

    In the second example, you can use one color to paint all the elements.

    In the third example, one possible way to paint the elements in 4 colors is:

    • paint in the first color the elements: a4=4 a6=2 and a7=2,
    • paint in the second color the elements: a2=6, a5=3 and a8=3,
    • paint in the third color the element a3=5,
    • paint in the fourth color the element a1=7.

    题意解释:输入n个数,对一个数进行涂色时,被涂色的数的倍数也会被涂色。输出最少涂几次可以涂完所有的数

    思路和筛法是一样的,从小的往大的筛,直到筛完为止。

    #include <bits/stdc++.h>
    using namespace std;
    int a[105];
    int main()
    {
        int n;
        cin>>n;
        for(int i=0;i<n;++i)
        {
            int t;
            cin>>t;
            a[t]=t;
        }
        int ans=0;
        for(int i=1;i<=100;++i)
        {
            if(a[i])
            {
                for(int j=i;j<=100;j+=i)
                {
                    a[j]=0;
                }
                ans++;
            }
        }
        cout<<ans;
        return 0;
    }
    View Code
  • 相关阅读:
    由wifi吞吐量问题联想到的分治思维
    总结----调试问题套路(经验)
    常用指令备忘录----持续更新
    【mark】OS是否使用svc方式分开系统空间和用户空间的优劣
    转载----五种开源协议(GPL,LGPL,BSD,MIT,Apache)
    rt-thread 动态装载实现、优化
    转:嵌入式: jffs2,yaffs2,logfs,ubifs文件系统性能分析
    gcc ld 链接器相关知识,调试指令(程序员的自我修养----链接、装载与库)
    HTML5与CSS3经典代码
    jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)
  • 原文地址:https://www.cnblogs.com/yoududezongzi/p/11524277.html
Copyright © 2011-2022 走看看