int lnd_in_supply(struct lndstr *lp) { if (!opt_NOFOOD) { if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD)) return 0; } return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL); }
int lnd_supply_all(struct lndstr *lp) { int fail = 0; if (!opt_NOFOOD) fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD)); fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL)); return !fail; }
/** * Compute command */ void command_compute(char* line) { char cmd[MAX_BUFFER]; char key[MAX_BUFFER]; char func[MAX_BUFFER]; char arg1[MAX_BUFFER]; int argc = sscanf(line, "%s %s %s %s", cmd, func, key, arg1); if (argc < 3) { goto invalid; } MATRIX_GUARD(key); uint32_t result = 0; if (strcasecmp(func, "sum") == 0) { result = get_sum(m); } else if (strcasecmp(func, "trace") == 0) { result = get_trace(m); } else if (strcasecmp(func, "minimum") == 0) { result = get_minimum(m); } else if (strcasecmp(func, "maximum") == 0) { result = get_maximum(m); } else if (strcasecmp(func, "frequency") == 0) { result = get_frequency(m, atoll(arg1)); } else { goto invalid; } printf("%" PRIu32 "\n", result); return; invalid: puts("invalid arguments"); }
int lnd_could_be_supplied(struct lndstr *lp) { int res, food, food_needed, shells_needed, shells; if (!opt_NOFOOD) { food_needed = get_minimum(lp, I_FOOD); food = lp->lnd_item[I_FOOD]; if (food < food_needed) { res = s_commod((struct empobj *)lp, lp->lnd_item, I_FOOD, food_needed, lchr[lp->lnd_type].l_item[I_FOOD], 0); lp->lnd_item[I_FOOD] = food; if (!res) return 0; } } shells_needed = lchr[lp->lnd_type].l_ammo; shells = lp->lnd_item[I_SHELL]; if (shells < shells_needed) { res = s_commod((struct empobj *)lp, lp->lnd_item, I_SHELL, shells_needed, lchr[lp->lnd_type].l_item[I_SHELL], 0); lp->lnd_item[I_SHELL] = shells; if (!res) return 0; } return 1; }
float retrieve_saturation(unsigned int r, unsigned int g, unsigned int b) { float saturation; unsigned int max = get_maximum(r, g, b); unsigned int min = get_minimum(r, g, b); saturation = max - min; return saturation; }
/** * Saturation is calculates as below: * * S = max(R, G, B) − min(R, G, B) */ tFloat32 retrieve_saturation(tUInt r, tUInt g, tUInt b) { tFloat32 saturation; tUInt max = get_maximum(r, g, b); tUInt min = get_minimum(r, g, b); saturation = static_cast<tFloat32> (max - min); return saturation; }
RB_node* get_successor(RB_node *node, RB_node *nil){ if(node->right != nil){ return get_minimum(node->right, nil); }else{ RB_node *succ = node->parent; while(succ != nil && node == succ->right){ node = succ; succ = succ->parent; } return succ; } }
int main(){ // In production code, of course, we won't hardcode this struct Link * link = load("data.txt"); printf("Print as ordered by genome A\n"); recursive_print(link, 0); printf("\nPrint as ordered by genome B\n"); recursive_print(link, 1); printf("\nLowest position on genome A\n"); print_link(get_minimum(link, 0)); printf("\nLowest position on genome B\n"); print_link(get_minimum(link, 1)); printf("\nAll links with scores above 50\n"); recursive_print(winnow(link, 50), 0); return EXIT_SUCCESS; }
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int N, size; int* arr; int* output; scanf("%d",&N); arr = (int *) malloc(N * sizeof(int)); output = (int *) malloc(N * sizeof(int)); for(int arr_i = 0; arr_i < N; arr_i++) { scanf("%d", &arr[arr_i]); } qsort(arr, N, sizeof(int), compare); size = get_minimum(arr, output, N); print_output(output, size); return 0; }
// ******************************************************************* // Function print_employee_wages // // Purpose: This function will print out the gross // wages of each employee. // // Parameters: employees - the array of structures for employees // size - the number of employees to input // // Returns: Nothing (void) // // ******************************************************************* void print_employee_wages (struct employee employees[], int size) { int i; // To increment the loop float maximum[3] = {0}; // stores the maximum values float minimum[3] = {0}; // stores the minimum values float total[3] = {0}; // Totals to be used for the total and average // Declare functions called void get_totals (struct employee employees[], float total_array[], int size); void get_minimum (struct employee employees[], float minimum[], int size); void get_maximum (struct employee employees[], float maximum[], int size); // Print out header information for data to be displayed printf ("\n--------------------------------------------------------------\n"); printf ("Name\t\t\tClock#\tWage\tHours\tOT\tGross\n"); printf ("--------------------------------------------------------------\n"); // Print out employee information to the screen for (i = 0; i < size; ++i) { printf ("%s\t\t%06li\t%.2f\t%.1f\t%.1f\t%4.2f\n", employees[i].name ,employees[i].id_number, employees[i].wage, employees[i].hours, employees[i].overtime, employees[i].gross); } printf("\n"); // run the stats functions get_totals (employees, total, size); get_minimum (employees, minimum, size); get_maximum (employees, maximum, size); // Print out various stats printf ("--------------------------------------------------------------\n"); // Total of hours, overtime hours, and gross wages paid printf("Total:\t\t\t\t\t%.1f\t%.1f\t\%.2f\n", total[0], total[1], total[2]); // Average of hours, overtime hours, and gross wages paid printf("Average:\t\t\t\t%.1f\t%.1f\t\%.2f\n", total[0] / size, total[1] / size, total[2] / size); // Minimum hours, overtime, and gross printf("Minimum:\t\t\t\t%.1f\t%.1f\t\%.2f\n", minimum[0], minimum[1], minimum[2]); // Maximum hours, overtime, and gross printf("Maximum:\t\t\t\t%.1f\t%.1f\t\%.2f\n", maximum[0], maximum[1], maximum[2]); printf("\n"); }
/** * Compute command. */ void command_compute(char* line) { char cmd[MAX_BUFFER]; char key[MAX_BUFFER]; char func[MAX_BUFFER]; char arg1[MAX_BUFFER]; int argc = sscanf(line, "%s %s %s %s", cmd, func, key, arg1); if (argc < 3) { goto invalid; } MATRIX_GUARD(key); float result = 0; if (strcasecmp(func, "sum") == 0) { result = get_sum(m); } else if (strcasecmp(func, "trace") == 0) { result = get_trace(m); } else if (strcasecmp(func, "minimum") == 0) { result = get_minimum(m); } else if (strcasecmp(func, "maximum") == 0) { result = get_maximum(m); } else if (strcasecmp(func, "determinant") == 0) { result = get_determinant(m); } else if (strcasecmp(func, "frequency") == 0) { ssize_t count = get_frequency(m, atof(arg1)); printf("%zu\n", count); return; } else { goto invalid; } printf("%.2f\n", result); return; invalid: puts("invalid arguments"); }
//Returns the most frequently occuring element //or -1 if there is no such unique element int64_t get_mode(int64_t* vector) { if (g_existCalcs[arg1].modeFlag == 1) return g_existCalcs[arg1].mode; g_existCalcs[arg1].modeFlag = 1; //Mode of a uniform vector is any element within it if (g_length == 1 || g_vec_properties[arg1][0] == UNIFORM) { g_existCalcs[arg1].mode = vector[0]; return vector[0]; } //All vectors with unique values have no mode else if (g_vec_properties[arg1][0] == SEQUP || g_vec_properties[arg1][0] == SEQDOWN || g_vec_properties[arg1][0] == PRIME) { g_existCalcs[arg1].mode = -1; return -1; } int64_t max = get_maximum(vector); int64_t min = get_minimum(vector); if ((max - min) < 2147483648) //16gb can hold 2147483648 int64s //12gb can hold 1610612736 int64s //8gb can hold 1073741824 int64s //4gb can hold 536870912 int64s return fast_mode(vector, max, min); int64_t* result = cloned(vector); qsort(result, g_length, sizeof(int64_t), int64Ascend); int64_t curSpanCount = 0, hiSpanCount = 0, currentCheck = 0, repeatCount = 0, mode = 0; for (int64_t i = 0; i < g_length; i++) { if (currentCheck != result[i]) { currentCheck = result[i]; curSpanCount = 0; } curSpanCount++; if (curSpanCount > hiSpanCount) { repeatCount = 0; hiSpanCount = curSpanCount; mode = currentCheck; } else if (currentCheck != result[i+1] && curSpanCount == hiSpanCount) { repeatCount = 1; } } if (repeatCount > 0) { g_existCalcs[arg1].mode = -1; return -1; } g_existCalcs[arg1].mode = mode; return mode; }
void test_fft(size_t size, double time = 1.5) { const tick_value correction = 0;//calibrate_correction(); printf("> [%9ld, ", (long)size); real* in = aligned_malloc<real>(size * 2); real* out = aligned_malloc<real>(size * 2); fill_random(in, size * 2); std::copy(in, in + size * 2, out); fft_benchmark<false> fft(size, out, out); fft.execute(); fill_random(in, size * 2); std::vector<tick_value> measures; const tick_value bench_tick_start = tick(); const time_value bench_start = now(); while (time_between(now(), bench_start) < time) { const tick_value start = tick(); fft.execute(); const tick_value stop = tick(); const tick_value diff = stop - start; measures.push_back(diff >= correction ? diff - correction : 0); fill_random(in, size * 2); dont_optimize(out); std::copy(in, in + size * 2, out); } const tick_value bench_tick_stop = tick(); const time_value bench_stop = now(); const double tick_frequency = (long double)(bench_tick_stop - bench_tick_start) / time_between(bench_stop, bench_start); tick_value tick_value = get_minimum(measures); double time_value = tick_value / tick_frequency; const double flops = (5.0 * size * std::log((double)size) / (std::log(2.0) * time_value)); const char* units = "'s' "; if (time_value < 0.000001) { units = "'ns'"; time_value = time_value * 1000000000.0; } else if (time_value < 0.001) { units = "'us'"; time_value = time_value * 1000000.0; } else if (time_value < 1.0) { units = "'ms'"; time_value = time_value * 1000.0; } printf("%9lld, ", tick_value); printf("%7g, %s, ", time_value, units); printf("%7g, ", flops / 1000000.0); printf("%8lld, ", measures.size()); printf("%8.6f],\n", tick_frequency / 1000000000.0); aligned_free(in); aligned_free(out); }
/* * Actually get the commod * * First, try to forage in the sector * Second look for a warehouse or headquarters to leech * Third, look for a ship we own in a harbor * Fourth, look for supplies in a supply unit we own * (one good reason to do this last is that the supply * unit will then call resupply, taking more time) * * May want to put code to resupply with SAMs here, later --ts */ static int s_commod(struct empobj *sink, short *vec, i_type type, int wanted, int limit, int actually_doit) { natid own = sink->own; coord x = sink->x; coord y = sink->y; int lookrange; struct sctstr sect; struct nstr_sect ns; struct nstr_item ni; struct lchrstr *lcp; struct shpstr ship; struct lndstr land; /* leave at least 1 military in sectors/ships */ int minimum = 0; int can_move; double move_cost, weight, mobcost; int packing; struct dchrstr *dp; struct ichrstr *ip; if (wanted > limit) wanted = limit; if (wanted <= vec[type]) return 1; wanted -= vec[type]; /* try to get it from sector we're in */ if (sink->ef_type != EF_SECTOR) { getsect(x, y, §); if (sect.sct_own == own) { if (!opt_NOFOOD && type == I_FOOD) minimum = 1 + (int)ceil(food_needed(sect.sct_item, etu_per_update)); if (sect.sct_item[type] - wanted >= minimum) { sect.sct_item[type] -= wanted; if (actually_doit) { vec[type] += wanted; putsect(§); put_empobj(sink->ef_type, sink->uid, sink); } return 1; } else if (sect.sct_item[type] - minimum > 0) { wanted -= sect.sct_item[type] - minimum; sect.sct_item[type] = minimum; if (actually_doit) { vec[type] += sect.sct_item[type] - minimum; putsect(§); } } } } /* look for a headquarters or warehouse */ lookrange = tfact(own, 10.0); snxtsct_dist(&ns, x, y, lookrange); while (nxtsct(&ns, §) && wanted) { if (ns.curdist == 0) continue; if (sect.sct_own != own) continue; if ((sect.sct_type != SCT_WAREH) && (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR)) continue; if ((sect.sct_type == SCT_HEADQ) && (sect.sct_dist_x == sect.sct_x) && (sect.sct_dist_y == sect.sct_y)) continue; if (sect.sct_effic < 60) continue; move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE); if (move_cost < 0) continue; if (!opt_NOFOOD && type == I_FOOD) minimum = 1 + (int)ceil(food_needed(sect.sct_item, etu_per_update)); if (sect.sct_item[type] <= minimum) continue; ip = &ichr[type]; dp = &dchr[sect.sct_type]; packing = ip->i_pkg[dp->d_pkg]; if (packing > 1 && sect.sct_effic < 60) packing = 1; weight = (double)ip->i_lbs / packing; mobcost = move_cost * weight; if (mobcost > 0) can_move = (double)sect.sct_mobil / mobcost; else can_move = sect.sct_item[type] - minimum; if (can_move > sect.sct_item[type] - minimum) can_move = sect.sct_item[type] - minimum; if (can_move >= wanted) { int n; sect.sct_item[type] -= wanted; /* take off mobility for delivering sect */ n = roundavg(wanted * weight * move_cost); sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil); if (actually_doit) { vec[type] += wanted; putsect(§); put_empobj(sink->ef_type, sink->uid, sink); } return 1; } else if (can_move > 0) { int n; wanted -= can_move; sect.sct_item[type] -= can_move; /* take off mobility for delivering sect */ n = roundavg(can_move * weight * move_cost); sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil); if (actually_doit) { vec[type] += can_move; putsect(§); } } } /* look for an owned ship in a harbor */ snxtitem_dist(&ni, EF_SHIP, x, y, lookrange); while (nxtitem(&ni, &ship) && wanted) { if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid) continue; if (ship.shp_own != own) continue; if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY)) continue; getsect(ship.shp_x, ship.shp_y, §); if (sect.sct_type != SCT_HARBR) continue; if (sect.sct_effic < 2) continue; move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE); if (move_cost < 0) continue; if (!opt_NOFOOD && type == I_FOOD) minimum = 1 + (int)ceil(food_needed(ship.shp_item, etu_per_update)); if (ship.shp_item[type] <= minimum) continue; ip = &ichr[type]; dp = &dchr[sect.sct_type]; packing = ip->i_pkg[dp->d_pkg]; if (packing > 1 && sect.sct_effic < 60) packing = 1; weight = (double)ip->i_lbs / packing; mobcost = move_cost * weight; if (mobcost > 0) can_move = (double)sect.sct_mobil / mobcost; else can_move = ship.shp_item[type] - minimum; if (can_move > ship.shp_item[type] - minimum) can_move = ship.shp_item[type] - minimum; if (can_move >= wanted) { int n; ship.shp_item[type] -= wanted; n = roundavg(wanted * weight * move_cost); sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil); if (actually_doit) { vec[type] += can_move; putship(ship.shp_uid, &ship); if (n) putsect(§); put_empobj(sink->ef_type, sink->uid, sink); } return 1; } else if (can_move > 0) { int n; wanted -= can_move; ship.shp_item[type] -= can_move; n = roundavg(can_move * weight * move_cost); sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil); if (actually_doit) { vec[type] += can_move; putship(ship.shp_uid, &ship); if (n) putsect(§); } } } /* look for an owned supply unit */ snxtitem_dist(&ni, EF_LAND, x, y, lookrange); while (nxtitem(&ni, &land) && wanted) { int min; if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid) continue; if (land.lnd_own != own) continue; lcp = &lchr[(int)land.lnd_type]; if (!(lcp->l_flags & L_SUPPLY)) continue; if (land.lnd_item[type] <= get_minimum(&land, type)) continue; if (land.lnd_ship >= 0) { getsect(land.lnd_x, land.lnd_y, §); if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2) continue; } move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE); if (move_cost < 0) continue; #if 0 /* * Recursive supply is disabled for now. It can introduce * cycles into the "resupplies from" relation. The code below * attempts to break these cycles by temporarily zapping the * commodity being supplied. That puts the land file in a * funny state temporarily, risking loss of supplies when * something goes wrong on the way. Worse, it increases * lnd_seqno even when !actually_doit, which can lead to * spurious seqno mismatch oopses in users of * lnd_could_be_supplied(). I can't be bothered to clean up * this mess right now, because recursive resupply is too dumb * to be really useful anyway: each step uses the first source * it finds, without consideration of mobility cost. If you * re-enable it, don't forget to uncomment its documentation * in supply.t as well. */ if (land.lnd_item[type] - wanted < get_minimum(&land, type)) { struct lndstr save; /* * Temporarily zap this unit's store, so the recursion * avoids it. */ save = land; land.lnd_item[type] = 0; putland(land.lnd_uid, &land); save.lnd_seqno = land.lnd_seqno; s_commod((struct empobj *)&land, land.lnd_item, type, wanted, lchr[land.lnd_type].l_item[type] - wanted, actually_doit); land.lnd_item[type] += save.lnd_item[type]; if (actually_doit) putland(land.lnd_uid, &land); else putland(save.lnd_uid, &save); } #endif min = get_minimum(&land, type); ip = &ichr[type]; weight = ip->i_lbs; mobcost = move_cost * weight; if (mobcost > 0) can_move = (double)land.lnd_mobil / mobcost; else can_move = land.lnd_item[type] - min; if (can_move > land.lnd_item[type] - min) can_move = land.lnd_item[type] - min; if (can_move >= wanted) { land.lnd_item[type] -= wanted; land.lnd_mobil -= roundavg(wanted * weight * move_cost); if (actually_doit) { vec[type] += wanted; putland(land.lnd_uid, &land); put_empobj(sink->ef_type, sink->uid, sink); } return 1; } else if (can_move > 0) { wanted -= can_move; land.lnd_item[type] -= can_move; land.lnd_mobil -= roundavg(can_move * weight * move_cost); if (actually_doit) { vec[type] += can_move; putland(land.lnd_uid, &land); } } } if (actually_doit) put_empobj(sink->ef_type, sink->uid, sink); return 0; }
void recursive_print(Link * link, size_t order_by){ recursive_print_r(get_minimum(link, order_by)->p[order_by]); }
int main() { scanf("%s", string); n = strlen(string); power[0] = 1; for (int i = 1; i <= n; ++ i) { power[i] = power[i - 1] * MAGIC; } hash[n] = 0; for (int i = n - 1; i >= 0; -- i) { hash[i] = hash[i + 1] * MAGIC + string[i]; } construct(); //for (int i = 0; i < n; ++ i) { // printf("%s\n", string + array[i]); //} for (int i = 0; i < n; ++ i) { minimum[0][i] = array[i]; } for (int j = 1; j < D; ++ j) { for (int i = 0; i + (1 << j) <= n; ++ i) { minimum[j][i] = std::min(minimum[j - 1][i], minimum[j - 1][i + (1 << j - 1)]); } } log[0] = 0; for (int i = 1; i <= n; ++ i) { log[i] = log[i - 1]; if (1 << log[i] + 1 < i) { log[i] ++; } } // height [0..n) for (int i = 0; i < n - 1; ++ i) { begin[i] = i - 1; while (begin[i] != -1 && height[begin[i]] >= height[i]) { begin[i] = begin[begin[i]]; } } for (int i = n - 2; i >= 0; -- i) { end[i] = i + 1; while (end[i] != n - 1 && height[end[i]] >= height[i]) { end[i] = end[end[i]]; } } for (int i = 0; i < n; ++ i) { answer[n - i] = Data(i, i); } //for (int i = 0; i < n; ++ i) { // printf("%s\n", string + array[i]); //} for (int i = 0; i + 1 < n; ++ i) { int l = begin[i] + 1; int r = end[i]; int a = get_minimum(l, i + 1); int b = get_minimum(i + 1, r + 1); if (a > b) { std::swap(a, b); } Data d(a, b); answer[height[i]] = std::min(answer[height[i]], d); } int q; scanf("%d", &q); while (q --) { int l; scanf("%d", &l); if (answer[l].i == -1) { puts("-1 -1"); } else { printf("%d %d\n", answer[l].i + 1, answer[l].j + 1); } } return 0; }