CSU- summer training title Candy Box (easy version)

Topic link: http: //codeforces.com/problemset/problem/1183/D

Ideas:

 The number of times I started using a method first appeared with a storage array value of this title is really tortured death of me, then the number of times an array of records and then b appears that the number of times, that is, the value of the number in the following table, and then from after forward traversal, if the number of times is greater than 1, it is added to the former, and finally out. Later, I would also like a lot of ways, time out, I finally give up direct b value, with this sort of approach. The method then I think that first appeared with a number of storage arrays value, then descending order, and then record the number of times the current flag to be added, then a traverse an array, if the value is greater than flag, flag on the increase, if less than flag , to increase the value, in fact, this approach is correct, but still timeout, I began to suspect that life, and finally, I found myself went wrong, overtime I use memset function caused this function to write easy, but It is better for fast cycle, for the local loop can be assigned.

AC Code

#include <the iostream> 
#include <CString> 
#include <algorithm>
 the using  namespace STD;
 const  int MAX + 2E5 = 20 is ;
 int A [MAX];
 BOOL CMP ( int X, int Y) { return X> Y;}
 int main () 
{ 
    int n-; 
    CIN >> n-;
     the while (N-- ) 
    { 
        int NUM; 
        CIN >> NUM;
     //     Memset (a, 0, the sizeof (a)); the not be used indiscriminately, because it is overrun time 
        for(int i=0;i<=num;i++)a[i]=0;
        int t;
        int ans=0;
        int maxNum=0;
        for(int i=1;i<=num;i++)
        {
            cin>>t;
            a[t]++;
            if(t>maxNum)maxNum=t;
        }
        sort(a,a+maxNum+1,cmp);
        int flag=a[0];
        for(int i=0;i<=maxNum;i++)
        {
            if(a[i]<=0||flag<=0)break;
            if(flag>a[i])flag=a[i];
            ans+=flag;
            flag--;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xlbfxx/p/11244509.html