a0df672a95 2013-12-04 kinaba: #include <iostream> a0df672a95 2013-12-04 kinaba: #include <sstream> a0df672a95 2013-12-04 kinaba: #include <iomanip> a0df672a95 2013-12-04 kinaba: #include <vector> a0df672a95 2013-12-04 kinaba: #include <string> a0df672a95 2013-12-04 kinaba: #include <map> a0df672a95 2013-12-04 kinaba: #include <set> a0df672a95 2013-12-04 kinaba: #include <algorithm> a0df672a95 2013-12-04 kinaba: #include <numeric> a0df672a95 2013-12-04 kinaba: #include <iterator> a0df672a95 2013-12-04 kinaba: #include <functional> a0df672a95 2013-12-04 kinaba: #include <complex> a0df672a95 2013-12-04 kinaba: #include <queue> a0df672a95 2013-12-04 kinaba: #include <stack> a0df672a95 2013-12-04 kinaba: #include <cmath> a0df672a95 2013-12-04 kinaba: #include <cassert> a0df672a95 2013-12-04 kinaba: #include <tuple> a0df672a95 2013-12-04 kinaba: using namespace std; a0df672a95 2013-12-04 kinaba: typedef long long LL; a0df672a95 2013-12-04 kinaba: typedef complex<double> CMP; a0df672a95 2013-12-04 kinaba: a0df672a95 2013-12-04 kinaba: class BinPacking { public: a0df672a95 2013-12-04 kinaba: int minBins(vector <int> item) a0df672a95 2013-12-04 kinaba: { a0df672a95 2013-12-04 kinaba: sort(item.begin(), item.end()); a0df672a95 2013-12-04 kinaba: return rec(item); a0df672a95 2013-12-04 kinaba: } a0df672a95 2013-12-04 kinaba: a0df672a95 2013-12-04 kinaba: int rec(vector<int>& item) a0df672a95 2013-12-04 kinaba: { a0df672a95 2013-12-04 kinaba: if(item.empty()) a0df672a95 2013-12-04 kinaba: return 0; a0df672a95 2013-12-04 kinaba: if(item.back() == 100) a0df672a95 2013-12-04 kinaba: return (item.size()+2)/3; a0df672a95 2013-12-04 kinaba: int w = item.back(); a0df672a95 2013-12-04 kinaba: item.pop_back(); a0df672a95 2013-12-04 kinaba: a0df672a95 2013-12-04 kinaba: int i = -1; a0df672a95 2013-12-04 kinaba: for(int k=0; k<item.size(); ++k) a0df672a95 2013-12-04 kinaba: if(item[k] + w <= 300) a0df672a95 2013-12-04 kinaba: i = k; a0df672a95 2013-12-04 kinaba: if(i!=-1) a0df672a95 2013-12-04 kinaba: item.erase(item.begin() + i); a0df672a95 2013-12-04 kinaba: return 1 + rec(item); a0df672a95 2013-12-04 kinaba: } a0df672a95 2013-12-04 kinaba: a0df672a95 2013-12-04 kinaba: }; a0df672a95 2013-12-04 kinaba: a0df672a95 2013-12-04 kinaba: // BEGIN CUT HERE a0df672a95 2013-12-04 kinaba: #include <ctime> a0df672a95 2013-12-04 kinaba: double start_time; string timer() a0df672a95 2013-12-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } a0df672a95 2013-12-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) a0df672a95 2013-12-04 kinaba: { os << "{ "; a0df672a95 2013-12-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) a0df672a95 2013-12-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } a0df672a95 2013-12-04 kinaba: void verify_case(const int& Expected, const int& Received) { a0df672a95 2013-12-04 kinaba: bool ok = (Expected == Received); a0df672a95 2013-12-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; a0df672a95 2013-12-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } a0df672a95 2013-12-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); a0df672a95 2013-12-04 kinaba: #define END verify_case(_, BinPacking().minBins(item));} a0df672a95 2013-12-04 kinaba: int main(){ a0df672a95 2013-12-04 kinaba: a0df672a95 2013-12-04 kinaba: CASE(0) a0df672a95 2013-12-04 kinaba: int item_[] = {150, 150, 150, 150, 150}; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = 3; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: CASE(1) a0df672a95 2013-12-04 kinaba: int item_[] = {130, 140, 150, 160}; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = 2; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: CASE(2) a0df672a95 2013-12-04 kinaba: int item_[] = {100, 100, 100, 100, 100, 100, 100, 100, 100}; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = 3; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: CASE(3) a0df672a95 2013-12-04 kinaba: int item_[] = {100, 200, 100, 100, 100, 100, 200, 100, 200}; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = 4; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: CASE(4) a0df672a95 2013-12-04 kinaba: int item_[] = {157, 142, 167, 133, 135, 157, 143, 160, 141, 123, 162, 159, 165, 137, 138, 152}; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = 8; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: /* a0df672a95 2013-12-04 kinaba: CASE(5) a0df672a95 2013-12-04 kinaba: int item_[] = ; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = ; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: CASE(6) a0df672a95 2013-12-04 kinaba: int item_[] = ; a0df672a95 2013-12-04 kinaba: vector <int> item(item_, item_+sizeof(item_)/sizeof(*item_)); a0df672a95 2013-12-04 kinaba: int _ = ; a0df672a95 2013-12-04 kinaba: END a0df672a95 2013-12-04 kinaba: */ a0df672a95 2013-12-04 kinaba: } a0df672a95 2013-12-04 kinaba: // END CUT HERE