Topcoder SRM 596 DIV 1

1188 단어
250 IncrementAndDoubling
class IncrementAndDoubling {
	private:
        int n;
        int a[55];
        bool ok(){
            for (int i=0;i<n;i++){
                if (a[i]!=0) return false;
            }
            return true;
        }
        int check(){
            int res=0;
            for (int i=0;i<n;i++){
                if (a[i]&1){
                    res+=1;
                    a[i]-=1;
                }
            }
            return res;
        }
        int doubi(){
            int res=0;
            for (int i=0;i<n;i++){
                if (a[i]){
                    a[i]/=2;
                    res=1;
                }
            }
            return res;
        }
	public:
	int getMin(vector <int> desiredArray) {
        int res=0;
        n=sz(desiredArray);
        for (int i=0;i<n;i++) a[i]=desiredArray[i];
        while (!ok()){
            res+=check();
            res+=doubi();
//            cerr<<res<<endl;
//            for (int i=0;i<n;i++) cerr<<a[i]<<" ";cerr<<endl;
//            cerr<<endl;
        }
        return res;
	}
};

좋은 웹페이지 즐겨찾기