// // Task 2: Equal Distribution // // C version. // #include #include #include #include #include #include #include "args.h" #include "parseints.h" #include "printarray.h" // // int pos = find_pair_diff_gt_1(nel, list[]); // Find the position pos of the first element of the first pair in list // where the absolute difference between list[pos] and list[pos+1] is > 1. // Return -1 if none. // int find_pair_diff_gt_1( int nel, int *list ) { for( int pos=0; pos 1 ) return pos; } return -1; } // // int pos = find_pair_f_lt_s(nel, list[]); // Find the position pos of the first element of a pair in @list // where list[$pos] < list[pos+1]. Return -1 if none. // int find_pair_f_lt_s( int nel, int *list ) { for( int pos=0; pos 1 elements\n" ); exit(1); } if( debug ) { printf( "debug: initial list: " ); print_int_array( 60, nel, list, ',', stdout ); putchar( '\n' ); } int nmoves = 0; if( debug ) { printf( "starting first pass\n" ); } // first pass: repeatedly find two adjacent cells whose absolute // difference > 1 and transfer one from the bigger to the smaller. int pos; while( (pos = find_pair_diff_gt_1(nel, list)) != -1 ) { if( debug ) { printf( "debug: found pos %d, list=", pos ); print_int_array( 60, nel, list, ',', stdout ); putchar( '\n' ); } if( list[pos] < list[pos+1] ) { list[pos]++; list[pos+1]--; } else { list[pos]--; list[pos+1]++; } nmoves++; } if( debug ) { printf( "starting second pass\n" ); } // second pass: repeatedly find 2 adjacent cells where // firstvalue < secondvalue and transfer one from secondvalue // to firstvalue while( (pos = find_pair_f_lt_s(nel, list)) != -1 ) { if( debug ) { printf( "debug: found pos %d, list=", pos ); print_int_array( 60, nel, list, ',', stdout ); putchar( '\n' ); } list[pos]++; list[pos+1]--; nmoves++; } int firstel = list[0]; int nsame = 0; for( pos=0; pos