Topcoder SRM 596 DIV 1



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;
	}
};


posted on 2013-11-21 12:31  电子幼体  阅读(132)  评论(0编辑  收藏  举报

导航