// Optimized quick sort algorithm void EDE_Browser::mqsort(char* arr[], int beg, int end, SortType type) { bool k = sort_direction ? false : true; if (end > beg + 1) { char* piv = arr[beg]; int l = beg + 1, r = end; while (l < r) { if (k^sortfn(arr[l],piv,type)) // ^ is XOR l++; else if (l==r-1) // avoid costly swap if they're the same r--; else { swap(--r,l); // Fl_Browser::swap() char *tmp=arr[l]; // update array arr[l]=arr[r]; arr[r]=tmp; } } // avoid costly swap if they're the same if (beg==l-1) l--; else { swap(--l,beg); char*tmp=arr[beg]; // update array arr[beg]=arr[l]; arr[l]=tmp; } // recursion mqsort(arr, beg, l, type); mqsort(arr, r, end, type); } }
void mqsort(infog *headp, int begin, int end) { int div; if( begin < end ) { div=par(headp, begin, end); mqsort(headp, begin, div-1); mqsort(headp, div+1, end); } }
/* sort input lines */ int main(int argc, char *argv[]) { int i, j, c, nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int reverse = 0; /* 1 if decreasing sort*/ int foldul = 0; /* 1 fold upper and lower case together */ for (i = 1; i < argc; i++) { if ((*++argv)[0] == '-') { for (j = 1; (c = (*argv)[j]) != '\0'; j++) { switch (c) { case 'n': numeric = 1; break; case 'r': reverse = 1; break; case 'f': foldul = 1; break; } } } } if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { if (numeric) { mqsort((void **) lineptr, 0, nlines-1, (int (*) (void *, void *))numcmp); } else if (foldul) { mqsort((void **) lineptr, 0, nlines-1, (int (*) (void *, void *))strcmp_ignoreCase); } else { mqsort((void **) lineptr, 0, nlines-1, (int (*) (void *, void *))strcmp); } if (reverse) { reverseF((void **)lineptr, nlines); } writelines(lineptr, nlines); for (i = 0; i < nlines; i++) { if (lineptr[i] != NULL) { free(lineptr[i]); } } return 0; } else { printf("input too big to sort\n"); return 1; } }
/* main */ int main(void) { int i,j,k; int recmax=0; int last_recmax=0; int n,door_size,maxt; infog forsort[101]; infog *ihead; scanf("%d %d %d", &n, &door_size, &maxt); memset(forsort, 0, sizeof(forsort)); ihead = forsort; for(i=0; i<n; i++) scanf("%d", &forsort[i].time); for(i=0; i<n; i++) scanf("%d", &forsort[i].fortune); for(i=0; i<n; i++) scanf("%d", &forsort[i].size); /* sort */ mqsort(forsort, 0, n-1); for(i=n-1; i>=0; i--) { if( (ihead[i].size > ihead[i].time) || (ihead[i].size==0) ) { ihead[i].best = 0; continue; } ihead[i].best = ihead[i].fortune; last_recmax = recmax; for(j=i+1; j<n; j++) { if( abs(ihead[i].size-ihead[j].size) <= abs(ihead[i].time-ihead[j].time) ) { if( (ihead[i].fortune+ihead[j].best) > ihead[i].best ) { ihead[i].best = ihead[i].fortune+ihead[j].best; } if( (ihead[i].fortune+ihead[j].best) >= recmax ) { recmax = ihead[i].fortune+ihead[j].best; ihead[i].best = recmax; } } } if( ihead[i].best >= recmax) { recmax=ihead[i].best; } } printf("%d\n", recmax); return 0; }
/* mqsort: sort v[left]...v[right] into increasing order */ void mqsort(void *v[], int left, int right, int (*comp) (void *, void *)) { int i, last; void swap(void *v[], int, int); if (left >= right) { /* do nothing if array contains */ return; /* fewer than two elements */ } swap(v, left, (left + right)/2); last = left; for (i = left+1; i <= right; i++) { if ((*comp)(v[i], v[left]) < 0) { swap(v, ++last, i); } } swap(v, left, last); mqsort(v, left, last-1, comp); mqsort(v, last+1, right, comp); }
void EDE_Browser::sort(int column, SortType type, bool reverse) { char *h=column_header_; int hlen=strlen(h); char colchar = Fl_Icon_Browser::column_char(); // FIXME sort() shouldn't call column_header() because that calls show_header() and that // deletes all buttons from header (so they could be recreated). This cause valgrind errors // since sort is called in button callback - you can't delete a widget in its own callback // Remove old sort direction symbol (if any) from header char *delim = 0; int col=0; if (sort_type != NO_SORT) { bool found=false; while ((delim=strchr(h, colchar))) { if (col==sort_column) { for (uint i=0; i<=strlen(delim); i++) delim[i-SYMLEN+1]=delim[i]; found=true; break; } h=delim+1; col++; } if (!found && col==sort_column) column_header_[hlen-SYMLEN+1]='\0'; h=column_header_; delim = 0; col=0; } // Add new symbol char *newheader = new char[hlen+6]; if (type != NO_SORT) { // Construct symbol char sym[SYMLEN]; if (reverse) snprintf(sym,SYMLEN,"@-22<"); else snprintf(sym,SYMLEN,"@-22>"); // Find column bool found=false; while ((delim=strchr(h, colchar))) { if (col==column) { *delim='\0'; snprintf(newheader,hlen+SYMLEN,"%s%s\t%s",column_header_,sym,delim+1); found=true; break; } h=delim+1; col++; } if (!found && col==column) // Just append symbol to string snprintf(newheader, hlen+SYMLEN,"%s%s",column_header_,sym); } else { strncpy (newheader, column_header_, hlen); newheader[hlen]='\0'; } column_header(newheader); delete[] newheader; sort_column=column; sort_type=type; sort_direction=reverse; // Start actually sorting if (type != NO_SORT) { // First create an array of strings in a given column char** sorttext = (char**)malloc(sizeof(char*)*(size()+1)); // index starts from 1 for simplicity in mqsort for (int i=1;i<=size();i++) { char *tmp = strdup(text(i)); char *l = tmp; int col=0; while ((delim=strchr(l, Fl_Icon_Browser::column_char()))) { *delim = '\0'; if (col==column) break; l=delim+1; col++; } sorttext[i] = strdup(l); free(tmp); } mqsort(sorttext, 1, size()+1, type); redraw(); // Free the allocated memory for (int i=1; i<=size(); i++) free (sorttext[i]); free(sorttext); } }
/* main */ int main(void) { int i,j,k; int recmax=0; int last_recmax=0; int n,door_size,maxt; int blocks; infog *ihead; infog *forsort; scanf("%d", &blocks); for(k=0; k<blocks; k++) { scanf("%d %d %d", &n, &door_size, &maxt); forsort = (infog *)malloc((n+1)*sizeof(infog)); memset(forsort, 0, (n+1)*sizeof(infog)); forsort[n].best=0; ihead = forsort; for(i=0; i<n; i++) scanf("%d", &forsort[i].time); for(i=0; i<n; i++) scanf("%d", &forsort[i].fortune); for(i=0; i<n; i++) scanf("%d", &forsort[i].size); /* sort */ mqsort(forsort, 0, n-1); recmax=0; for(i=n-1; i>=0; i--) { if( (ihead[i].size > ihead[i].time) || (ihead[i].size==0) ) { ihead[i].best = 0; continue; } ihead[i].best = ihead[i].fortune; last_recmax = recmax; for(j=i+1; j<n; j++) { if( abs(ihead[i].size-ihead[j].size) <= abs(ihead[i].time-ihead[j].time) ) { if( (ihead[i].fortune+ihead[j].best) > ihead[i].best ) { ihead[i].best = ihead[i].fortune+ihead[j].best; } if( (ihead[i].fortune+ihead[j].best) >= recmax ) { recmax = ihead[i].fortune+ihead[j].best; ihead[i].best = recmax; } } } if( ihead[i].best > recmax) recmax=ihead[i].best; } free(ihead); if(k < blocks-1) printf("%d\n\n", recmax); else printf("%d\n", recmax); } return 0; }