#if 1 #include #include #include //#define TEST/* UNDEFINE IT IN RELEASE VER */ unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; char *SEQ_DIG_FOUND = NULL; #ifdef TEST #include unsigned int find_func_times = 0; #endif #define ABS_IN_K(a,b) ((a>K?(a-K):0) <= b && a+K >= b) unsigned int find(unsigned int pre, unsigned int *input, unsigned int *output_pos) { #ifdef TEST find_func_times++; #endif while(input <= SEQUENCE+N-1) { if (ABS_IN_K(pre,*input)) {/* a right digit is found */ unsigned int len = 1; unsigned int pos = input-SEQUENCE; *output_pos = pos; *(SEQ_DIG_FOUND+pos) = '\1'; /* mark it found before */ while (ABS_IN_K(*input,*(input+1)) && (N-pos)>1) { pos = input+1-SEQUENCE; *(output_pos+len) = pos; *(SEQ_DIG_FOUND+pos) = '\1'; /* mark it found before */ input++; len++; } if ((N-pos)>1) { /* result of taking the digit into account */ unsigned int len = 1+find(*input, input+1, output_pos+1); unsigned int len_new; unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*(N-pos)); memset(output_new_pos, 0, sizeof(unsigned int)*(N-pos)); /* result of ignoring the digit*/ len_new = find(pre, input+1, output_new_pos); /* compare the output with the right digit and without */ if (len <= len_new) { memcpy(output_pos, output_new_pos, sizeof(unsigned int)*len_new); len = len_new; } free(output_new_pos); return len; } else return len; } input++; }; return 0; } unsigned int i = 0; unsigned int *final_output_pos = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; unsigned int *temp = NULL; //#define N 6 //#define K 3 //unsigned int SEQUENCE[N] = {2, 9, 7, 2, 5, 8}; /*9 7 5 8*/ 7 //unsigned int SEQUENCE[N] = {3, 3, 3, 3, 3, 3}; /*3 3 3 3 3 3*/ 5 //unsigned int SEQUENCE[N] = {9, 2, 5, 1, 1, 1}; /*2 1 1 1*/ 8 //unsigned int SEQUENCE[N] = {2, 5, 8, 1, 1, 1}; /*2 1 1 1*/ 4 //unsigned int SEQUENCE[N] = {1, 1, 1, 8, 5, 2}; /*1 1 1 2*/ //#define N 3 //#define K 3 //unsigned int SEQUENCE[N] = {2, 5, 1}; /*2 5*/ 2 //#define N 10 //#define K 3 //unsigned int SEQUENCE[N] = {2, 5, 1, 7, 90, 100, 3, 6, 8, 2}; /*2 5 7 6 8*/ 20 int main(int argc, char *argv[]) { #if !defined(TEST) // clock_t start,finish; // start = clock(); scanf("%d %d", &N, &K); SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); #else clock_t start,finish; start = clock(); N=5000;K=1; //20:524287(0.374) 21:1048575(0.765) 22:2097151(1.462) 23:4194303(3.064) 24:8388607(5.999) 25:16777215(16.201) //20:19 100:99 1000:999 10000: SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i; i= final_output_len) { temp = final_output_pos; final_output_pos = output_pos; output_pos = temp; final_output_len = output_len; } } for (i=final_output_len;i>0;i--) { printf("%d ", *(SEQUENCE+*(final_output_pos+i-1))); } #ifdef TEST finish = clock(); printf("\n%d %f", find_func_times, (double)(finish-start)/CLOCKS_PER_SEC); #endif return 0; /* return here, free will use cpu time */ free(SEQUENCE); free(SEQ_DIG_FOUND); free(final_output_pos); free(output_pos); return 0; } #elif 0 #include #include #include #define TEST/* UNDEFINE IT IN RELEASE VER */ unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; char *SEQ_DIG_FOUND = NULL; #ifdef TEST #include unsigned int find_func_times = 0; #endif unsigned int find(unsigned int pre, unsigned int *input, unsigned int *output_pos) { unsigned int pos = input-SEQUENCE; #ifdef TEST find_func_times++; #endif if ((pre>K?(pre-K):0) <= *input && pre+K >= *input) {/* a right digit is found */ *output_pos = pos; *(SEQ_DIG_FOUND+pos) = '\1'; /* mark it found before */ if ((N-pos)>1) { if (!((*(input+1)>K?(*(input+1)-K):0) <= *input && *(input+1)+K >= *input)) { /* result of taking the digit into account */ unsigned int len = 1+find(*input, input+1, output_pos+1); unsigned int len_new; unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*(N-pos)); memset(output_new_pos, 0, sizeof(unsigned int)*(N-pos)); /* result of ignoring the digit*/ len_new = find(pre, input+1, output_new_pos); /* compare the output with the right digit and without */ if (len <= len_new) { memcpy(output_pos, output_new_pos, sizeof(unsigned int)*len_new); len = len_new; } free(output_new_pos); return len; } else return 1+find(*input, input+1, output_pos+1); } else return 1; } else {/* not found */ if ((N-pos)>1) return find(pre, input+1, output_pos); else return 0; } } unsigned int i = 0; unsigned int *final_output_pos = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; unsigned int *temp = NULL; //#if 0 //#define N 6 //#define K 3 // //unsigned int SEQUENCE[N] = {2, 9, 7, 2, 5, 8}; /*9 7 5 8*/ 19 //unsigned int SEQUENCE[N] = {3, 3, 3, 3, 3, 3}; /*3 3 3 3 3 3*/ 6:31 7:63 8:127 9:255 10:511 20:524287(0.374) // one not in 6:17 7:35 8:71 9:143 10:287 20:262655 // quan wu guan 6:16 7:22 8:29 9:37 10:46 20:191 //unsigned int SEQUENCE[N] = {9, 2, 5, 1, 1, 1}; /*2 1 1 1*/ 19 //unsigned int SEQUENCE[N] = {2, 5, 6, 1, 1, 1}; /*2 1 1 1*/ 15 //#elif 0 //#define N 3 //#define K 3 // //unsigned int SEQUENCE[N] = {2, 5, 1}; /*2 5*/ 3 //#elif 0 //#define N 10 //#define K 3 // //unsigned int SEQUENCE[N] = {2, 5, 1, 7, 90, 100, 3, 6, 8, 2}; /*2 5 7 6 8*/ 55 //#endif int main(int argc, char *argv[]) { #if !defined(TEST) scanf("%d %d", &N, &K); SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); #else clock_t start,finish; start = clock(); N=5000;K=1; //20:524287(0.374) 21:1048575(0.765) 22:2097151(1.462) 23:4194303(3.064) 24:8388607(5.999) 25:16777215(16.201) //20:19 100:99 1000:999 10000: SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i; i= final_output_len) { temp = final_output_pos; final_output_pos = output_pos; output_pos = temp; final_output_len = output_len; } } for (i=final_output_len;i>0;i--) { printf("%d ", *(SEQUENCE+*(final_output_pos+i-1))); } #ifdef TEST finish = clock(); printf("\n%d %f", find_func_times, (double)(finish-start)/CLOCKS_PER_SEC); #endif return 0; /* return here, free will use cpu time */ free(SEQUENCE); free(SEQ_DIG_FOUND); free(final_output_pos); free(output_pos); return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #elif 0 #include #include /* define n and k */ #if 0 #define N 6 #define K 3 unsigned int SEQUENCE[N] = {2, 9, 7, 2, 5, 8}; /*9 7 5 8*/ //unsigned int SEQUENCE[N] = {9, 2, 5, 1, 1, 1}; /*2 1 1 1*/ //unsigned int SEQUENCE[N] = {9, 2, 5, 1, 4, 1}; /*2 5 4 1*/ #elif 0 #define N 3 #define K 3 unsigned int SEQUENCE[N] = {2, 5, 1}; /*2 5*/ #elif 0 #define N 10 #define K 3 unsigned int SEQUENCE[N] = {2, 5, 1, 7, 90, 100, 3, 6, 8, 2}; /*2 5 7 6 8*/ #endif unsigned int get_output_len(unsigned int *output) { unsigned int j; for (j=0;jout1_len) { return out2_len; } else if (out2_len==out1_len) { unsigned int j; for(j=out1_len;j>0;j--) { if(*(out2_pos+j-1) < *(out1_pos+j-1)) { return out2_len; } } } return 0; } void printout(unsigned int *out) { unsigned int i; for (i=0;(*(out+i)!=0 && ik?(pre-k):0) <= *input && pre + k >= *input) {/* a right digit is found */ unsigned int output_new[N]; unsigned int output_new_pos[N]; unsigned int output_new_len; memset(output_new, 0, sizeof(output_new)); memset(output_new_pos, 0, sizeof(output_new_pos)); /* result of taking the digit into account */ *output = *input; *output_pos = pos; find(*output, size-1, k, input+1, pos+1, output+1, output_pos+1); /* result of ignoring the digit*/ find(pre, size-1, k, input+1, pos+1, output_new, output_new_pos); /* compare the output with the right digit and without */ output_new_len = compareout(output, output_pos, output_new, output_new_pos); if (output_new_len > 0) { memcpy(output, output_new, sizeof(unsigned int)*output_new_len); memcpy(output_pos, output_new_pos, sizeof(unsigned int)*output_new_len); } } else {/* not found */ find(pre, size-1, k, input+1, pos+1, output, output_pos); } } int main(int argc, char *argv[]) { unsigned int i; unsigned int final_output[N]; unsigned int final_output_pos[N]; unsigned int *input; memset(final_output, 0, sizeof(final_output)); memset(final_output_pos, 0, sizeof(final_output_pos)); for (i=0; i < N; i++) { unsigned int output_len = 0; unsigned int output[N]; unsigned int output_pos[N]; memset(output, 0, sizeof(output)); memset(output_pos, 0, sizeof(output_pos)); input = SEQUENCE + i; *output = *input; *output_pos = i; find(*output, N-(i+1), K, input+1, i+1, output+1, output_pos+1); /* compare the found one for i with current longest one */ output_len = compareout(final_output, final_output_pos, output, output_pos); if (output_len > 0) { memcpy(final_output, output, sizeof(unsigned int)*output_len); memcpy(final_output_pos, output_pos, sizeof(unsigned int)*output_len); } } printout(final_output); return 0; } #elif 0 #include #include #include unsigned int N, K; unsigned int *SEQUENCE; unsigned int get_output_len(unsigned int *output) { unsigned int j; for (j=0;jout1_len) { return out2_len; } else if (out2_len==out1_len) { unsigned int j; for(j=out1_len;j>0;j--) { if(*(out2_pos+j-1) < *(out1_pos+j-1)) { return out2_len; } } } return 0; } void printout(unsigned int *out) { unsigned int i; for (i=0;(*(out+i)!=0 && ik?(pre-k):0) <= *input && pre + k >= *input) {/* a right digit is found */ unsigned int *output_new; unsigned int *output_new_pos; unsigned int output_new_len; output_new = (unsigned int *)malloc(sizeof(unsigned int)*N); output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(output_new, 0, sizeof(unsigned int)*N); memset(output_new_pos, 0, sizeof(unsigned int)*N); /* result of taking the digit into account */ *output = *input; *output_pos = pos; find(*output, size-1, k, input+1, pos+1, output+1, output_pos+1); /* result of ignoring the digit*/ find(pre, size-1, k, input+1, pos+1, output_new, output_new_pos); /* compare the output with the right digit and without */ output_new_len = compareout(output, output_pos, output_new, output_new_pos); if (output_new_len > 0) { memcpy(output, output_new, sizeof(unsigned int)*output_new_len); memcpy(output_pos, output_new_pos, sizeof(unsigned int)*output_new_len); } free(output_new); free(output_new_pos); } else {/* not found */ find(pre, size-1, k, input+1, pos+1, output, output_pos); } } int main(int argc, char *argv[]) { unsigned int i; unsigned int *final_output; unsigned int *final_output_pos; unsigned int *input; unsigned int *output; unsigned int *output_pos; scanf("%d %d", &N, &K); if(!(N>=1 && N<=100000)) return -1; if(!(K>=1 && K<=100000)) return -2; SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); for (i=0; i0)) return -3; } final_output = (unsigned int *)malloc(sizeof(unsigned int)*N); final_output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(final_output, 0, sizeof(unsigned int)*N); memset(final_output_pos, 0, sizeof(unsigned int)*N); output = (unsigned int *)malloc(sizeof(unsigned int)*N); output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i=0; i < N; i++) { unsigned int output_len = 0; memset(output, 0, sizeof(unsigned int)*N); memset(output_pos, 0, sizeof(unsigned int)*N); input = SEQUENCE + i; *output = *input; *output_pos = i; find(*output, N-(i+1), K, input+1, i+1, output+1, output_pos+1); /* compare the found one for i with current longest one */ output_len = compareout(final_output, final_output_pos, output, output_pos); if (output_len > 0) { memcpy(final_output, output, sizeof(unsigned int)*output_len); memcpy(final_output_pos, output_pos, sizeof(unsigned int)*output_len); } } printout(final_output); free(final_output); free(final_output_pos); free(output); free(output_pos); return 0; } #elif 0 #include #include #include unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; /* return out2's length if out1 shall be replaced with out2, otherwise return 0 */ unsigned int compareout(unsigned int *out1_pos, unsigned int *out2_pos) { unsigned int j = 0; unsigned int len = 0; for (;j0;j--) { if(*(out2_pos+j-1) < *(out1_pos+j-1)) { return len; } else if(*(out2_pos+j-1) > *(out1_pos+j-1)) { return 0; } } return 0; */ return len; } } else { if(*(out2_pos+j)==0) return 0; else { if(*(out2_pos+j) < *(out1_pos+j)) { len = j+1; } else if(*(out2_pos+j) > *(out1_pos+j)) { len = 0; } else { if(len!=0) len++; } } } } for (j++;jk?(pre-k):0) <= *input && pre + k >= *input) {/* a right digit is found */ unsigned int output_new_len; unsigned int *output_new = (unsigned int *)malloc(sizeof(unsigned int)*size); unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*size); memset(output_new, 0, sizeof(unsigned int)*size); memset(output_new_pos, 0, sizeof(unsigned int)*size); /* result of taking the digit into account */ *output = *input; *output_pos = pos+1; find(*output, size-1, k, input+1, pos+1, output+1, output_pos+1); /* result of ignoring the digit*/ find(pre, size-1, k, input+1, pos+1, output_new, output_new_pos); /* compare the output with the right digit and without */ output_new_len = compareout(output_pos, output_new_pos); if (output_new_len > 0) { memcpy(output, output_new, sizeof(unsigned int)*output_new_len); memcpy(output_pos, output_new_pos, sizeof(unsigned int)*output_new_len); } free(output_new); free(output_new_pos); } else {/* not found */ find(pre, size-1, k, input+1, pos+1, output, output_pos); } } unsigned int i = 0; unsigned int *final_output = NULL; unsigned int *final_output_pos = NULL; unsigned int *output = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; int main(int argc, char *argv[]) { scanf("%d %d", &N, &K); /* no need to check, rule says "the program can assume that input data is always valid and consistent with specification given in the problem description" if(!(N>=1 && N<=100000)) return -1; if(!(K>=1 && K<=100000)) return -2; */ SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); for (i; i0)) // return -3; } final_output = (unsigned int *)malloc(sizeof(unsigned int)*N); final_output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(final_output, 0, sizeof(unsigned int)*N); memset(final_output_pos, 0, sizeof(unsigned int)*N); output = (unsigned int *)malloc(sizeof(unsigned int)*N); output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i=0; i < N && final_output_len < N - i; i++) { memset(output, 0, sizeof(unsigned int)*N); memset(output_pos, 0, sizeof(unsigned int)*N); *output = *(SEQUENCE + i); *output_pos = i+1; find(*output, N-(i+1), K, SEQUENCE+i+1, i+1, output+1, output_pos+1); /* compare the found one for i with current longest one */ output_len = compareout(final_output_pos, output_pos); if (output_len > 0) { memcpy(final_output, output, sizeof(unsigned int)*output_len); memcpy(final_output_pos, output_pos, sizeof(unsigned int)*output_len); final_output_len = output_len; } } for (i=0;i #include #include #define COUNT_FIND /* UNDEFINE IT IN RELEASE VER */ unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; unsigned int *SEQ_DIG_FOUND = NULL; #ifdef COUNT_FIND unsigned int find_func_times = 0; unsigned int compare_func_times = 0; #endif /* return out2's length if out1 shall be replaced with out2, otherwise return 0 */ unsigned int compareout(unsigned int *out1_pos, unsigned int *out2_pos) { unsigned int j = 0; unsigned int len = 0; #ifdef COUNT_FIND compare_func_times++; #endif for (;j *(out1_pos+j)) { len = 0; } else { if(len!=0) len++; } } } } for (j++;jk?(pre-k):0) <= *input && pre + k >= *input) {/* a right digit is found */ *output = *input; *output_pos = pos+1; *(SEQ_DIG_FOUND+pos) = 1; /* mark it found before */ if (size>1) { unsigned int output_new_len; unsigned int *output_new = (unsigned int *)malloc(sizeof(unsigned int)*size); unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*size); memset(output_new, 0, sizeof(unsigned int)*size); memset(output_new_pos, 0, sizeof(unsigned int)*size); /* result of taking the digit into account */ find(*output, size-1, k, input+1, pos+1, output+1, output_pos+1); /* result of ignoring the digit*/ find(pre, size-1, k, input+1, pos+1, output_new, output_new_pos); /* compare the output with the right digit and without */ output_new_len = compareout(output_pos, output_new_pos); if (output_new_len > 0) { memcpy(output, output_new, sizeof(unsigned int)*output_new_len); memcpy(output_pos, output_new_pos, sizeof(unsigned int)*output_new_len); } free(output_new); free(output_new_pos); } } else {/* not found */ if (size>1) find(pre, size-1, k, input+1, pos+1, output, output_pos); } } unsigned int i = 0; unsigned int *final_output = NULL; unsigned int *final_output_pos = NULL; unsigned int *output = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; int main(int argc, char *argv[]) { scanf("%d %d", &N, &K); /* no need to check, rule says "the program can assume that input data is always valid and consistent with specification given in the problem description" if(!(N>=1 && N<=100000)) return -1; if(!(K>=1 && K<=100000)) return -2; */ SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); SEQ_DIG_FOUND = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQ_DIG_FOUND, 0, sizeof(unsigned int)*N); for (i; i0)) // return -3; } final_output = (unsigned int *)malloc(sizeof(unsigned int)*N); final_output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(final_output, 0, sizeof(unsigned int)*N); memset(final_output_pos, 0, sizeof(unsigned int)*N); output = (unsigned int *)malloc(sizeof(unsigned int)*N); output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i=0; i < N && final_output_len < N - i && *(SEQ_DIG_FOUND+i) != 1; i++) { memset(output, 0, sizeof(unsigned int)*N); memset(output_pos, 0, sizeof(unsigned int)*N); *output = *(SEQUENCE + i); *output_pos = i+1; find(*output, N-(i+1), K, SEQUENCE+i+1, i+1, output+1, output_pos+1); /* compare the found one for i with current longest one */ output_len = compareout(final_output_pos, output_pos); if (output_len > 0) { memcpy(final_output, output, sizeof(unsigned int)*output_len); memcpy(final_output_pos, output_pos, sizeof(unsigned int)*output_len); final_output_len = output_len; } } for (i=0;i #include #include #define COUNT_FIND /* UNDEFINE IT IN RELEASE VER */ unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; unsigned int *SEQ_DIG_FOUND = NULL; #ifdef COUNT_FIND unsigned int find_func_times = 0; unsigned int compare_func_times = 0; #endif /* return out2's length if out1 shall be replaced with out2, otherwise return 0 */ unsigned int compareout(unsigned int *out1_pos, unsigned int *out2_pos) { unsigned int j = 0; unsigned int len = 0; #ifdef COUNT_FIND compare_func_times++; #endif for (;j *(out1_pos+j)) { len = 0; } else { if(len!=0) len++; } } } } for (j++;jK?(pre-K):0) <= *input && pre + K >= *input) {/* a right digit is found */ *output_pos = pos+1; *(SEQ_DIG_FOUND+pos) = 1; /* mark it found before */ if (size>1) { unsigned int output_new_len; unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*size); memset(output_new_pos, 0, sizeof(unsigned int)*size); /* result of taking the digit into account */ find(*input, size-1, input+1, pos+1, output_pos+1); /* result of ignoring the digit*/ find(pre, size-1, input+1, pos+1, output_new_pos); /* compare the output with the right digit and without */ output_new_len = compareout(output_pos, output_new_pos); if (output_new_len > 0) { memcpy(output_pos, output_new_pos, sizeof(unsigned int)*output_new_len); } free(output_new_pos); } } else {/* not found */ if (size>1) find(pre, size-1, input+1, pos+1, output_pos); } } unsigned int i = 0; unsigned int *final_output_pos = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; int main(int argc, char *argv[]) { scanf("%d %d", &N, &K); /* no need to check, rule says "the program can assume that input data is always valid and consistent with specification given in the problem description" if(!(N>=1 && N<=100000)) return -1; if(!(K>=1 && K<=100000)) return -2; */ SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); SEQ_DIG_FOUND = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQ_DIG_FOUND, 0, sizeof(unsigned int)*N); for (i; i0)) // return -3; } final_output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(final_output_pos, 0, sizeof(unsigned int)*N); output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i=0; i < N && final_output_len < N - i && *(SEQ_DIG_FOUND+i) != 1; i++) { memset(output_pos, 0, sizeof(unsigned int)*N); *output_pos = i+1; find(*(SEQUENCE + i), N-(i+1), SEQUENCE+i+1, i+1, output_pos+1); /* compare the found one for i with current longest one */ output_len = compareout(final_output_pos, output_pos); if (output_len > 0) { memcpy(final_output_pos, output_pos, sizeof(unsigned int)*output_len); final_output_len = output_len; } } for (i=0;i #include #include #define COUNT_FIND /* UNDEFINE IT IN RELEASE VER */ unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; unsigned int *SEQ_DIG_FOUND = NULL; #ifdef COUNT_FIND unsigned int find_func_times = 0; unsigned int compare_func_times = 0; #endif unsigned int j = 0; unsigned int len = 0; /* return out2's length if out1 shall be replaced with out2, otherwise return 0 */ unsigned int compareout(unsigned int *out1_pos, unsigned int *out2_pos) { j = 0; len = 0; #ifdef COUNT_FIND compare_func_times++; #endif for (;j *(out1_pos+j)) { len = 0; } else { if(len!=0) len++; } } } } for (j++;jK?(pre-K):0) <= *input && pre+K >= *input) {/* a right digit is found */ *output_pos = pos+1; *(SEQ_DIG_FOUND+pos) = 1; /* mark it found before */ if ((N-pos)>1) { unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*(N-pos)); memset(output_new_pos, 0, sizeof(unsigned int)*(N-pos)); /* result of taking the digit into account */ find(*input, input+1, pos+1, output_pos+1); /* result of ignoring the digit*/ find(pre, input+1, pos+1, output_new_pos); /* compare the output with the right digit and without */ compareout(output_pos, output_new_pos); free(output_new_pos); } } else {/* not found */ if ((N-pos)>1) find(pre, input+1, pos+1, output_pos); } } unsigned int i = 0; unsigned int *final_output_pos = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; int main(int argc, char *argv[]) { scanf("%d %d", &N, &K); /* no need to check, rule says "the program can assume that input data is always valid and consistent with specification given in the problem description" if(!(N>=1 && N<=100000)) return -1; if(!(K>=1 && K<=100000)) return -2; */ SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); SEQ_DIG_FOUND = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQ_DIG_FOUND, 0, sizeof(unsigned int)*N); for (i; i0)) // return -3; } final_output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(final_output_pos, 0, sizeof(unsigned int)*N); output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i=0; i < N && final_output_len < N - i && *(SEQ_DIG_FOUND+i) != 1; i++) { memset(output_pos, 0, sizeof(unsigned int)*N); *output_pos = i+1; find(*(SEQUENCE + i), SEQUENCE+i+1, i+1, output_pos+1); /* compare the found one for i with current longest one */ output_len = compareout(final_output_pos, output_pos); if (output_len > 0) { final_output_len = output_len; } } for (i=0;i #include #include #define COUNT_FIND /* UNDEFINE IT IN RELEASE VER */ unsigned int N = 0, K = 0; unsigned int *SEQUENCE = NULL; char *SEQ_DIG_FOUND = NULL; #ifdef COUNT_FIND unsigned int find_func_times = 0; #endif //#if 0 //#define N 6 //#define K 3 // //unsigned int SEQUENCE[N] = {2, 9, 7, 2, 5, 8}; /*9 7 5 8*/ 19 //unsigned int SEQUENCE[N] = {3, 3, 3, 3, 3, 3}; /*3 3 3 3 3 3*/ 31 //unsigned int SEQUENCE[N] = {9, 2, 5, 1, 1, 1}; /*2 1 1 1*/ 19 //unsigned int SEQUENCE[N] = {2, 5, 6, 1, 1, 1}; /*2 1 1 1*/ 15 //#elif 0 //#define N 3 //#define K 3 // //unsigned int SEQUENCE[N] = {2, 5, 1}; /*2 5*/ 3 //#elif 0 //#define N 10 //#define K 3 // //unsigned int SEQUENCE[N] = {2, 5, 1, 7, 90, 100, 3, 6, 8, 2}; /*2 5 7 6 8*/ 55 //#endif unsigned int find(unsigned int pre, unsigned int *input, unsigned int pos, unsigned int *output_pos) { unsigned int len = 0; #ifdef COUNT_FIND find_func_times++; #endif if ((pre>K?(pre-K):0) <= *input && pre+K >= *input) {/* a right digit is found */ *output_pos = pos+1; *(SEQ_DIG_FOUND+pos) = '1'; /* mark it found before */ len++; if ((N-pos)>1) { unsigned int len_new = 0; unsigned int *output_new_pos = (unsigned int *)malloc(sizeof(unsigned int)*(N-pos)); memset(output_new_pos, 0, sizeof(unsigned int)*(N-pos)); /* result of taking the digit into account */ len += find(*input, input+1, pos+1, output_pos+1); /* result of ignoring the digit*/ len_new = find(pre, input+1, pos+1, output_new_pos); /* compare the output with the right digit and without */ if (len <= len_new) { memcpy(output_pos, output_new_pos, sizeof(unsigned int)*len_new); len = len_new; } free(output_new_pos); return len; } else return len; } else {/* not found */ if ((N-pos)>1) return len + find(pre, input+1, pos+1, output_pos); else return len; } } unsigned int i = 0; unsigned int *final_output_pos = NULL; unsigned int *output_pos = NULL; unsigned int output_len = 0; unsigned int final_output_len = 0; unsigned int *temp = NULL; int main(int argc, char *argv[]) { scanf("%d %d", &N, &K); SEQUENCE = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(SEQUENCE, 0, sizeof(unsigned int)*N); SEQ_DIG_FOUND = (char *)malloc(sizeof(char)*N); memset(SEQ_DIG_FOUND, 0, sizeof(char)*N); final_output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); memset(final_output_pos, 0, sizeof(unsigned int)*N); output_pos = (unsigned int *)malloc(sizeof(unsigned int)*N); for (i; i= final_output_len) { temp = final_output_pos; final_output_pos = output_pos; output_pos = temp; final_output_len = output_len; } } for (i=final_output_len;i>0;i--) { printf("%d ", *(SEQUENCE+*(final_output_pos+i-1)-1)); } #ifdef COUNT_FIND printf("\n%d", find_func_times); #endif return 0; /* return here, free will use cpu time */ free(SEQUENCE); free(SEQ_DIG_FOUND); free(final_output_pos); free(output_pos); return 0; } #endif