void GetLeastNumbers(const vector<int>& a, intset& leastNum, size_t k) { leastNum.clear(); if (k < 1 || a.size() < k) return; vector<int>::const_iterator iter = a.begin(); for (; iter != a.end(); iter++) { if ((leastNum.size()) < k) leastNum.insert(*iter); else { setiterator itergreater = leastNum.begin(); if (*iter < *(leastNum.begin())) { leastNum.erase(itergreater); leastNum.insert(*iter); } } } }
void solution2(const vector<int>& data, intset& leastNumbers, int k) { leastNumbers.clear(); if (k<1 && data.size()<k) return ; vector<int>::const_iterator iter = data.begin(); for (; iter != data.end(); ++iter) { if ((leastNumbers.size()) < k) leastNumbers.insert(*iter); else { setIterator iterBig = leastNumbers.begin(); if (*iter < *iterBig) { leastNumbers.erase(iterBig); leastNumbers.insert(*iter); } } } }
SatelliteProblemDomainGenerator(int n_sat, int n_inst_type, int n_ant_type, int n_gs, int n_tw) : num_satellites(n_sat) , num_ground_statiion(n_gs) , num_antenna_types(n_ant_type) , num_instrument_types(n_inst_type) , num_timewindows(n_tw) , instrument_max_on_time(80) , instrument_min_off_time(5) { if (num_antenna_types < 1) num_antenna_types = 1; if (num_instrument_types < 1) num_instrument_types = 1; srand(time(NULL)); for (int i = 0; i < num_antenna_types; i++) { ant_rate.push_back(get_rand_num(2, 5)); ant_bat_rate.push_back(get_rand_num(2, 5)); ant_angular_speed.push_back(get_rand_num(5, 10)); ant_gs_map.push_back(intvector(num_ground_statiion, 0)); ant_sat_map.push_back(intvector(num_satellites, 0)); antennaTotal.insert(antennaTotal.end(), i); } //std::cout << "Antenna Total Size = " << antennaTotal.size() << std::endl; for (int i = 0; i < num_instrument_types; i++) { inst_rate.push_back(get_rand_num(3, 8)); inst_bat_rate.push_back(get_rand_num(3, 8)); inst_angular_speed.push_back(get_rand_num(5, 10)); inst_sat_map.push_back(intvector(num_satellites, 0)); instrumentTotal.insert(instrumentTotal.end(), i); } for (int i = 0; i < num_satellites; i++) { sat_unit_recharge.push_back(get_rand_num(15, 20)); sat_storage_total.push_back(get_rand_num(150, 200)); sat_battery_total.push_back(get_rand_num(350, 500)); satellite_sun_visibility.push_back(std::vector<intpair>()); int start = get_rand_num(0, 100); int length = get_rand_num(20, 30); for (int j = 0; j < num_timewindows; j++) { satellite_sun_visibility[i].push_back(std::make_pair<int, int>(start, start + length)); start = start + length + 40; } /* * Each Satellite must support at least 2 instrument types */ int instrumentNumber = get_rand_num(1, num_instrument_types); intset instsize = instrumentTotal; while (instrumentNumber > 0) { int selected = get_rand_num(0, instsize.size() - 1); intset::iterator itr = instsize.begin(); int count = 0; while (itr != instsize.end()) { if (count == selected) { inst_sat_map[*itr][i] = 1; //std::cout << "Satellite " << i << " will have instrument " << *itr << " = " << inst_sat_map[*itr][i] << std::endl; instsize.erase(itr); instrumentNumber--; break; } itr++; count++; } } /* * Each Satellite must support at least 2 antenna types; */ int antNumber = get_rand_num(1, num_antenna_types); intset antsize = antennaTotal; while (antNumber > 0) { int selected = get_rand_num(0, antsize.size() - 1); intset::iterator itr = antsize.begin(); int count = 0; while (itr != antsize.end()) { if (count == selected) { ant_sat_map[*itr][i] = 1; //std::cout << "Satellite " << i << " will have antenna " << *itr << " = " << ant_sat_map[*itr][i] << std::endl; antsize.erase(itr); antNumber--; break; } itr++; count++; } } satellite_groundstation_visibility.push_back(std::vector< std::vector<intpair> >()); for (int j = 0; j < num_ground_statiion; j++) { satellite_groundstation_visibility[i].push_back(std::vector<intpair>()); int start = get_rand_num(0, 100); int length = get_rand_num(20, 30); for (int k = 0; k < num_timewindows; k++) { satellite_groundstation_visibility[i][j].push_back(std::make_pair<int, int>(start, start + length)); start = start + length + 60; } } } for (int i = 0; i < num_ground_statiion; i++) { gs_angular_distance.push_back(intvector()); for (int j = 0; j < num_ground_statiion; j++) { gs_angular_distance[i].push_back(0); if (i != j) { gs_angular_distance[i][j] = get_manhattan_dist(get_rand_num(0, 100), get_rand_num(0, 100), get_rand_num(0, 100), get_rand_num(0, 100)); } } /* * Each Ground Station must support at least 1 antenna type */ int antNumber = get_rand_num(1, num_antenna_types); intset antsize = antennaTotal; while (antNumber > 0) { int selected = get_rand_num(0, antsize.size() - 1); intset::iterator itr = antsize.begin(); int count = 0; while (itr != antsize.end()) { if (count == selected) { ant_gs_map[*itr][i] = 1; //std::cout << "Ground Station " << i << " will have antenna " << *itr << " = " << ant_gs_map[*itr][i] << std::endl; antsize.erase(itr); antNumber--; break; } itr++; count++; } } } domain_name << "Sat-" << n_sat << "-GS-" << n_gs << "-Inst-" << n_inst_type << "-Ant-" << n_ant_type; }