这个应该算比较简洁的了,呵呵,原来是数中间的间隔m-1段,这个思路很有意思:
/* ID: qq104801 LANG: C++ TASK: barn1 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> /* for debug only:counter */ void debug_dummy(void) { return; } int m,s,c; int bb[200],cc[200]; int cmpa(const void *a,const void *b) { return *(int*)a - *(int*)b; } int cmpd(const void *a,const void *b) { return *(int*)b - *(int*)a; } int test(void) { int total,i; if(m>=s) return c; else { cc[0]=0; for(i=1;i<c;i++) cc[i]=bb[i]-bb[i-1]-1; qsort(cc,c,sizeof(cc[0]),cmpd); //for(i=0;i<c;i++) //printf("%d\n",cc[i]); total=bb[i-1]-bb[0]+1; //printf("%d\n",total); for(int i=0;i<m-1;i++) total-=cc[i]; } return total; } main () { FILE *fin = fopen ("barn1.in", "r"); FILE *fout = fopen ("barn1.out", "w"); fscanf(fin,"%d %d %d",&m,&s,&c); for(int i=0;i<c;i++) { fscanf(fin,"%d",&bb[i]); } qsort(bb,c,sizeof(bb[0]),cmpa); int count=0; count=test(); //printf("%d\n",count); fprintf(fout,"%d\n",count); fclose(fin); fclose(fout); exit (0); }
测试用例:
USER: ll tom [qq104801] TASK: barn1 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.005 secs, 3496 KB] Test 2: TEST OK [0.003 secs, 3496 KB] Test 3: TEST OK [0.003 secs, 3496 KB] Test 4: TEST OK [0.005 secs, 3496 KB] Test 5: TEST OK [0.008 secs, 3496 KB] Test 6: TEST OK [0.003 secs, 3496 KB] Test 7: TEST OK [0.003 secs, 3496 KB] Test 8: TEST OK [0.008 secs, 3496 KB] Test 9: TEST OK [0.003 secs, 3496 KB] Test 10: TEST OK [0.003 secs, 3496 KB] All tests OK. Your program (‘barn1‘) produced all correct answers! This is your submission #2 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 4 50 17 3 4 6 8 14 15 16 17 25 26 27 30 31 40 41 42 43 ------- test 2 ---- 2 10 4 2 4 6 8 ------- test 3 ---- 3 27 16 2 3 5 6 8 9 10 13 14 15 16 19 20 21 22 27 ------- test 4 ---- 1 200 8 101 105 102 106 103 107 104 99 ------- test 5 ---- 50 200 10 18 69 195 38 73 28 6 172 53 99 ------- test 6 ---- 50 30 6 30 25 20 15 10 5 ------- test 7 ---- 20 200 80 65 178 64 70 18 32 88 90 98 20 152 31 118 117 127 81 175 73 136 161 165 63 130 133 190 10 4 138 200 43 189 37 86 182 145 110 67 126 114 153 99 25 155 119 176 55 48 197 62 147 125 60 12 23 112 96 27 122 35 50 36 49 149 108 100 188 77 191 6 121 166 132 82 95 150 89 22 40 128 56 ------- test 8 ---- 4 200 100 72 180 46 198 196 131 165 112 52 133 187 93 57 35 128 65 127 130 12 49 88 155 122 193 101 164 98 143 54 149 38 84 45 139 79 16 102 20 14 150 188 33 176 135 29 80 19 74 11 114 95 185 137 59 32 189 66 67 191 91 77 134 18 10 7 200 8 13 55 24 142 184 17 6 109 105 43 181 85 94 151 160 115 25 116 111 37 104 144 97 90 141 120 119 152 182 123 172 40 23 ------- test 9 ---- 20 195 100 1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 41 42 43 44 45 51 52 53 54 55 61 62 63 64 65 71 72 73 74 75 81 82 83 84 85 91 92 93 94 95 101 102 103 104 105 111 112 113 114 115 121 122 123 124 125 131 132 133 134 135 141 142 143 144 145 151 152 153 154 155 161 162 163 164 165 171 172 173 174 175 181 182 183 184 185 191 192 193 194 195 ------- test 10 ---- 1 200 2 1 200 Keep up the good work! Thanks for your submission!
原文:http://www.cnblogs.com/dpblue/p/3948166.html