int main(int argc, char *argv[]) { IMP::base::setup_from_argv(argc, argv, "Test of base caches in C++"); IMP_NEW(IMP::kernel::Model, m, ()); IMP::kernel::ParticleIndexes pis; IMP::algebra::BoundingBox3D bb = IMP::algebra::get_unit_bounding_box_d<3>(); for (unsigned int i = 0; i < num_particles; ++i) { pis.push_back(m->add_particle("P%1%")); IMP::core::XYZR::setup_particle( m, pis.back(), IMP::algebra::Sphere3D(IMP::algebra::get_random_vector_in(bb), radius)); } IMP_NEW(IMP::core::GridClosePairsFinder, gcpf, ()); gcpf->set_distance(.1); IMP::base::PointerMember<IMP::core::ClosePairsFinder> mcpf = IMP::misc::create_metric_close_pairs_finder(LowerBound(), UpperBound()); mcpf->set_distance(.1); IMP::kernel::ParticleIndexPairs gcp = gcpf->get_close_pairs(m, pis); canonicalize(gcp); IMP::kernel::ParticleIndexPairs mcp = mcpf->get_close_pairs(m, pis); canonicalize(mcp); std::sort(gcp.begin(), gcp.end()); std::sort(mcp.begin(), mcp.end()); std::cout << "Lists are " << gcp << " and " << mcp << std::endl; IMP::kernel::ParticleIndexPairs out; std::set_intersection(gcp.begin(), gcp.end(), mcp.begin(), mcp.end(), std::back_inserter(out)); IMP_TEST_EQUAL(out.size(), mcp.size()); IMP_TEST_EQUAL(out.size(), gcp.size()); return 0; }
void init(int* pCakeArray,int nCakeCnt){ assert(pCakeArray != NULL); assert(nCakeCnt>0); //复制烙饼数 m_nCakeCnt = nCakeCnt; //复制烙饼数组 m_CakeArray = (int*)malloc(nCakeCnt*sizeof(int)); assert(m_CakeArray != NULL); int i; for(i=0;i<nCakeCnt;i++){ m_CakeArray[i] = pCakeArray[i]; } //得到最大交换次数 m_nMaxSwap = UpperBound(m_nCakeCnt); //初始化交换结果数组 m_SwapArray = (int*)malloc((m_nMaxSwap+1)*sizeof(int)); assert(m_SwapArray !=NULL); //初始化中间交换结果信息 m_ReverseArray = (int*)malloc(m_nCakeCnt*sizeof(int)); assert(m_ReverseArray != NULL); for(i=0;i<nCakeCnt;i++){ m_ReverseArray[i] = pCakeArray[i]; } m_ReverseSwapArray = (int*)malloc(m_nMaxSwap*sizeof(int)); }
/////////////////////////////////////////////////////////////////////////////// // CreateEntry // /////////////////////////////////////////////////////////////////////////////// void cHierDatabaseIter::CreateEntry( const TSTRING& name ) //throw (eArchive, eHierDatabase) { cHierEntry newEntry; newEntry.mName = name; // // insert this in order in the set... // mIter = UpperBound( newEntry.mName.c_str() ); // // make sure that no duplicate names are added to the database... // if( mIter != mEntries.end() ) { if( 0 == Compare( mIter->mName.c_str(), newEntry.mName.c_str() ) ) { return; //throw eHierDbDupeName( name ); } } // Note -- insert() inserts directly _before_ the iterator // we need to get the current address so we can set the new object's // next pointer appropriately // cHierAddr nextAddr; if( ! Done() ) { nextAddr = GetCurrentAddr(); } mIter = mEntries.insert( mIter, newEntry ); // // first, we should write the new object to the database // mIter->mNext = nextAddr; cHierAddr newAddr = util_WriteObject( mpDb, &(*mIter) ); // // set the previous object's next pointer, and rewrite that object to disk... // if( mIter == mEntries.begin() ) { // we need to rewrite the array info... // mInfo.mArray = newAddr; util_RewriteObject( mpDb, &mInfo, mInfoAddr ); } else { // altering the previous node... // mIter--; mIter->mNext = newAddr; util_RewriteObject( mpDb, &(*mIter), GetCurrentAddr() ); mIter++; } }
/////////////////////////////////////////////////////////////////////////////// // SeekTo /////////////////////////////////////////////////////////////////////////////// bool cHierDatabaseIter::SeekTo(const TCHAR* pName) { EntryArray::iterator i = UpperBound( pName ); if( i != mEntries.end() ) { if( 0 == Compare( pName, (*i).mName.c_str() ) ) { mIter = i; return true; } } mIter = mEntries.end(); return false; }
int solve_85() { const int UPPER_BOUND = UpperBound(); int area(0), maxCount(0); for(int i(1); i < UPPER_BOUND; ++i) for(int j(i); j < UPPER_BOUND; ++j) { const int RECT_COUNT = RectCount(i, j); if(RECT_COUNT > RECT_LIMIT) break; if(RECT_COUNT < RECT_LIMIT && RECT_COUNT > maxCount) { maxCount = RECT_COUNT; area = i * j; } } return area; }
//--------------------------------------------------------------------------- void __fastcall TfKnapsack::btBranchAndBoundClick(TObject *Sender) { //如果已存在 ndHeapArray 先清除 if (ndHeapArray) { delete[] ndHeapArray; ndHeapArray = NULL; } //讀入背包限制 iBagCapacity = StrToInt(edBagCapacity->Text); //產生 Heap 陣列 iHeapCount = 0; ndHeapArray = new Node[iTotalNum +1]; //產生 Heap 的第一個節點 Node u, v; u.iLevel = -1; u.iNowProfit = 0; u.iNowWeight = 0; u.iUpperBound = UpperBound(u); u.iLowerBound = LowerBound(u); InsertHeap(u); Item *itm; int iNowUpper = INT_MAX; while (iHeapCount > 0) { u = DeleteHeap(); if (u.iLevel == iTotalNum-1) continue; v.iLevel = u.iLevel +1; itm = wrItems[v.iLevel].itm; //現在的重量加下一個 Item 的重量沒超過限重 //才去計算 UpperBound 與 LowerBound if (u.iNowWeight + itm->iWeight <= iBagCapacity) { //拿這個東西 v.iNowProfit = u.iNowProfit + itm->iProfit; v.iNowWeight = u.iNowWeight + itm->iWeight; v.iUpperBound = UpperBound(v); v.iLowerBound = LowerBound(v); if (v.iUpperBound > v.iLowerBound && v.iLowerBound < iNowUpper) InsertHeap(v); if (v.iUpperBound < iNowUpper) iNowUpper = v.iUpperBound; } //不拿這個東西 v.iNowProfit = u.iNowProfit; v.iNowWeight = u.iNowWeight; v.iUpperBound = UpperBound(v); v.iLowerBound = LowerBound(v); if (v.iUpperBound > v.iLowerBound && v.iLowerBound < iNowUpper) InsertHeap(v); if (v.iUpperBound < iNowUpper) iNowUpper = v.iUpperBound; } memSolution->Lines->Add("----------------------------------------------"); memSolution->Lines->Add("Maximum possible profit:"); memSolution->Lines->Add(-iNowUpper); }