美文网首页
CUC-SUMMER-1-Q

CUC-SUMMER-1-Q

作者: Nioge | 来源:发表于2017-07-31 22:51 被阅读0次
    Q - Beautiful Paintings
    CodeForces - 651B

    There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.

    We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

    Input
    The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.

    The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

    Output
    Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

    Example
    Input
    5
    20 30 10 50 40
    Output
    4
    Input
    4
    200 100 100 200
    Output
    2
    Note
    In the first sample, the optimal order is: 10, 20, 30, 40, 50.

    In the second sample, the optimal order is: 100, 200, 100, 200.


    题意:排列使相邻两个数后数大于前数这种情况最多

    解法:桶排序思想,创建数组a[1000],初始化为0,输入i,使a[i]++,判断不重复的序列有几个,每个序列元素个数减1,再相加即为最后答案

    代码:

    #include<iostream>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
        int num,s=0;
        cin>>num;
        int a[1000];
        for(int i=0;i<1000;i++)
            a[i]=0;
        for(int i=0;i<num;i++){
            int b;
            cin>>b;
            a[b-1]++;
        }
        for(int i=0;i<1000;i++){
            int temp=0;
            int flag=1;
            for(int j=0;j<1000;j++){
                if(a[j]>0){
                    a[j]-=1;
                    temp++;
                    flag=-1;
                }
            }
            s=s+temp-1;
            if(flag==1){
                s+=1;
                break;
            }
        }
        cout<<s<<endl;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:CUC-SUMMER-1-Q

          本文链接:https://www.haomeiwen.com/subject/zrrdlxtx.html