/* Subject: Contest submission for problem #2, file 2.cc */ /* cory_m@imail.EECS.Berkeley.EDU */ /* Thu Sep 11 05:30:10 PDT 2003 */ /*___CONTEST_SUBMISSION___ cory_m 2 */ //#include #include #include #include bool enumeratePermutations(int n, int k, int j[]); struct list_perm { int x; struct list_perm * next; struct list_perm * prev; }; typedef struct list_perm permutation; struct all_perm { permutation *x; struct all_perm * next; }; typedef struct all_perm permList; permutation* makePerm(int permSize, int permArray[13]) { int i; permutation* perm= (permutation*)malloc(sizeof(permutation)); permutation* permStart=perm; permutation* permPtr=perm; for(i=0;inext = perm; perm->prev=permPtr; permPtr->x = permArray[i]; permPtr = perm; } perm->x=permArray[i]; perm->next=permStart; permStart->prev=perm; while((permStart->x)!=1){ permStart=permStart->next; } return permStart; } void freePerm(permutation* a) { while((a->x)!=1){ a=a->next; } a=a->next; permutation* curr = a; permutation* next= a->next; free(curr); while(next->x!=1){ curr=next; next=next->next; free(curr); } free(next); } bool comparePerm(permutation* a,permutation* b){ while(a->x!=1) a=a->next; while(b->x!=1) b=b->next; //compare if a=b bool same = true; a=a->next; b=b->next; while((a->x)!=1) { if((a->x)!=(b->x)) same = false; a=a->next; b=b->next; } if(same) return true; //compare if a = b reverse while(a->x!=1) a=a->next; while(b->x!=1) b=b->next; a=a->next; b=b->prev; while((a->x)!=1) { if((a->x)!=(b->x)) return false; a=a->next; b=b->prev; } return true; } void addPerm(permList * root,permutation *a){ permList* perm= (permList*)malloc(sizeof(permList)); perm->x=a; perm->next=NULL; while(root->next != NULL) root =root->next; root->next=perm; } bool findPerm(permutation* y , permList * list) { do{ if(comparePerm(list->x,y)) return true; list=list->next; }while(list != NULL); return false; } bool checkPerm(int size,int a[13] , int maxSum){ for(int i=0;imaxSum) return false; if(a[0]+a[size-2]+a[size-1]>maxSum) return false; if(a[0]+a[1]+a[size-1]>maxSum) return false; return true; } void printPerm(permutation * a){ printf("%d ",a->x); a=a->next; while((a->x)!=1){ printf("%d ",a->x); a=a->next; } printf("\n"); } void printAllPerm(permList * root){ printPerm(root->x); root=root->next; while(root!=NULL){ printPerm(root->x); root=root->next; } } int countAllPerm(permList * root){ int num=1; root=root->next; while(root!=NULL){ num++; root=root->next; } return num; } int main(void) { //FILE *input=fopen("e.in","r"); int numRepeat, permSize, maxSum; //fscanf(input,"%d",&numRepeat); scanf("%d",&numRepeat); for(int k=0;knext=NULL; listRoot->x=first; permutation* newPerm; int comb[13] = {-1}; while(enumeratePermutations(permSize-1, permSize-1, comb)){ for(int j=0;j= 0 && j[i] >= j[i+1]; --i){} if(i < 0){ return false; } else { int least = i + 1; for(int m = i + 2; m < n; ++m){ if(j[m] < j[least] && j[m] > j[i]){ least = m; } } int t = j[i]; j[i] = j[least]; j[least] = t; if(k - 1 > i){ int start = i + 1; int end = n - 1; while(start < end){ t = j[start]; j[start++] = j[end]; j[end--] = t; } start = k; end = n - 1; while(start < end){ t = j[start]; j[start++] = j[end]; j[end--] = t; } } return true; } } }