int main(){ int test, ks, i, nxt, flag; while ( scanf("%d", &test) == 1 ){ for ( ks = 1 ; ks <= test ; ks++ ){ scanf("%d%d", &n, &k); flag = 1; for ( i = 0 ; i < n ; i++ ) scanf("%d", &arr[i]); qsort(arr, n, sizeof(int), cmp); res[n] = 0; for ( i = n - 1 ; i >= 0 ; i-- ){ res[i] = 0; nxt = lBound(arr, n, arr[i] + 2*k); if ( nxt - i >= 2 ){ res[i] = 1 + res[nxt+1]; if ( nxt - 1 - i >= 2 ) res[i] = MIN((1 + res[nxt]), res[i]); if ( nxt - 2 - i >= 2 ) res[i] = MIN((1 + res[nxt-1]), res[i]); } else res[i] = INF; } printf("Case %d: %d\n", ks, (res[0] < INF) ? res[0] : -1); } } return 0; }
void TextureBuilder::createModules() { QSharedPointer<ModuleDescriptor> mod; foreach (mod,_modDesc) { auto modPtr = mod.data(); if (this->useRandomFactors() && modPtr->enableRandom() ) { modPtr->setBias(this->applyRandomFactor(modPtr->bias()) ); modPtr->setDispl(this->applyRandomFactor(modPtr->displ()) ); modPtr->setExp(this->applyRandomFactor(modPtr->exp()) ); modPtr->setFreq(this->applyRandomFactor(modPtr->freq()) ); modPtr->setLac(this->applyRandomFactor(modPtr->lac()) ); modPtr->setLbound(this->applyRandomFactor(modPtr->lBound()) ); modPtr->setPers(this->applyRandomFactor(modPtr->pers()) ); modPtr->setPow(this->applyRandomFactor(modPtr->pow()) ); modPtr->setRough(this->applyRandomFactor(modPtr->rough()) ); modPtr->setScale(this->applyRandomFactor(modPtr->scale()) ); modPtr->setValue(this->applyRandomFactor(modPtr->value()) ); modPtr->setX(this->applyRandomFactor(modPtr->x()) ); modPtr->setY(this->applyRandomFactor(modPtr->y()) ); modPtr->setZ(this->applyRandomFactor(modPtr->z()) ); } mod.data()->setModules(_modules); auto ptr = mod.data()->makeModule(); qDebug() << "Module " << modPtr->name() << " After module creation..."; modPtr->dumpModule(); _modules.insert(mod.data()->name(), ptr); }
int Variable::genColumn( Active<Constraint, Variable> *actCon, Column &col) const { double eps = master_->machineEps(); double minusEps = -eps; int n = actCon->number(); expand(); for (int i = 0; i < n; i++) { double co = (*actCon)[i]->coeff(this); if (co > eps || co < minusEps) col.insert(i,co); } col.obj(obj()); col.lBound(lBound()); col.uBound(uBound()); compress(); return col.nnz(); }
void LpSub::initialize() { // LpSub::initialize(): local variables Array<double> obj(sub_->nVar()); Array<double> lBound(sub_->nVar()); Array<double> uBound(sub_->nVar()); Array<Row*> rows(sub_->nCon()); Array<LPVARSTAT::STATUS> lpVarStat(sub_->nVar()); Array<SlackStat::STATUS> slackStat(sub_->nCon()); Row row(master_, sub_->nVar()); //!< buffer to store generated row int conNnz; //!< number of nonzeros of constraint \a c int c; //!< loop index // generate the row format of the active constraints /* After the generation of the row format we allocate a row of * the correct length and make a copy in order to safe memory. */ int nRow = 0; const int nCon = sub_->nCon(); for (c = 0; c < nCon; c++) { conNnz = sub_->constraint(c)->genRow(sub_->actVar(), row); rows[nRow] = new Row(master_, conNnz); rows[nRow]->copy(row); slackStat[nRow] = sub_->slackStat(c)->status(); ++nRow; row.clear(); } // eliminate set and fixed variables and initialize the columns Variable *v; //!< pointer to variable of subproblem Array<bool> marked(0,sub_->nVar()-1, false); //!< \a true if variable can be eliminated nOrigVar_ = sub_->nVar(); valueAdd_ = 0.0; // LpSub: mark variables to eliminate, build objective function and bounds /* We mark all variables which can be eliminated, add them to the * ArrayBuffer \a delVar, compute the mappings from the original variable * set to the actual variable set in the \a LP, and vice versa, and * determine the correction term for the LP-value. * If all variables can be eliminated then we do not eliminate the last * variable for simpification. Otherwise it would be necessary to load * an problem with 0 variables to the LP-solver which is, e.g., for * Cplex not possible. Although the emulation of the optimization would * still be simple, but extra work would have to be performed if later * constraints were added. */ const int nVar = sub_->nVar(); int nCol = 0; for (int i = 0; i < nVar; i++) { v = sub_->variable(i); if(sub_->fsVarStat(i)->fixedOrSet()) { if (eliminable(i) && (nCol || (i != sub_->nVar() - 1))) { //! eliminate variable \a i from the LP marked[i] = true; valueAdd_ += v->obj() * elimVal(i); orig2lp_[i] = -1; } else { // fix variable \a i in the LP /* As variable \a i could not be eliminated we set both its upper and lower * bound to the value it is fixed or set to. */ orig2lp_[i] = nCol; lp2orig_[nCol] = i; obj[nCol] = v->obj(); lBound[nCol] = elimVal(i); uBound[nCol] = elimVal(i); lpVarStat[nCol] = sub_->lpVarStat(i)->status(); ++nCol; } } else { // add variable \a i to the LP orig2lp_[i] = nCol; lp2orig_[nCol] = i; obj[nCol] = v->obj(); lBound[nCol] = sub_->lBound(i); uBound[nCol] = sub_->uBound(i); lpVarStat[nCol] = sub_->lpVarStat(i)->status(); ++nCol; } } // LpSub: update the constraints /* If all active variables of a constraint are eliminated then * its left hand side is void (implicitly 0), but its right hand side * can be nonzero. Depending on the sense of the constraint it can be * infeasible. * If the elimination of variables from constraints causes an infeasible * \a LP, the constraint is memorized in \a infeasCons_. */ ArrayBuffer<int> delVar(sub_->nVar(),false); //!< buffer of deletable components of row format double rhsDelta; //!< correction of right hand side due to eliminations InfeasCon::INFEAS infeas; //!< infeasibility mode (TooLarge, TooSmall) for (c = 0; c < nCon; c++) { // eliminate the variables from the constraint delVar.clear(); rhsDelta = 0.0; const int rNnz = rows[c]->nnz(); for(int i = 0; i < rNnz; i++) if(marked[rows[c]->support(i)]) { delVar.push(i); rhsDelta += rows[c]->coeff(i)*elimVal(rows[c]->support(i)); } rows[c]->delInd(delVar, rhsDelta); // check if the constraint is now infeasible if (rows[c]->nnz() == 0) { infeas = sub_->constraint(c)->voidLhsViolated(rows[c]->rhs()); if (infeas != InfeasCon::Feasible) infeasCons_.push(new InfeasCon(master_, sub_->constraint(c), infeas)); } rows[c]->rename(orig2lp_); } // initialize the LP-solver and clean up LP::initialize(*master_->optSense(), nRow, sub_->maxCon(), nCol, sub_->maxVar(), obj, lBound, uBound, rows, lpVarStat, slackStat); for (c = 0; c < nCon; c++) delete rows[c]; }