fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include <stdint.h>
  5. #include <float.h>
  6. #include <time.h>
  7. #include <sys/time.h>
  8.  
  9. #define SEED64 88172645463325252ULL
  10.  
  11. typedef double val_t;
  12.  
  13. inline static double baka_sec_count(void) {
  14. struct timeval tv;
  15. gettimeofday(&tv, NULL);
  16. return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
  17.  
  18. }
  19.  
  20. static uint64_t x = SEED64;
  21.  
  22. inline void seed64(uint64_t seed) {
  23. x = seed;
  24. }
  25.  
  26. inline static uint64_t rand64(void) {
  27. x = x ^ (x << 13);
  28. x = x ^ (x >> 7);
  29. return x = x ^ (x << 17);
  30. }
  31.  
  32. inline static val_t* alloc_data(int n_) {
  33. val_t* p = (val_t*)malloc(sizeof(val_t) * n_);
  34.  
  35. for (int i = 0; i < n_; ++i) {
  36. p[i] = rand64();
  37. }
  38.  
  39. return p;
  40. }
  41.  
  42. inline static val_t* alloc_clone_data(double* p_, int n_) {
  43. val_t* p = (val_t*)malloc(sizeof(val_t) * n_);
  44. memcpy(p, p_, sizeof(val_t) * n_);
  45. return p;
  46. }
  47.  
  48. inline static void free_data(double* p_) {
  49. free(p_);
  50. }
  51.  
  52. // ahoselect ここから
  53.  
  54. inline static void aho_select(val_t* arr_, int n_, val_t* top_, int k_) {
  55.  
  56. for (int i = 0; i < k_; ++i) {
  57. top_[i] = -DBL_MAX;
  58. }
  59.  
  60. for (int i = 0; i < n_; ++i) {
  61. for (int j = 0; j < k_; ++j) {
  62. if (top_[j] < arr_[i]) {
  63. for (int l = k_ - 1; l > j; --l) {
  64. top_[l] = top_[l - 1];
  65. }
  66. top_[j] = arr_[i];
  67. break;
  68. }
  69. }
  70. }
  71. }
  72.  
  73. // ahoselect ここまで
  74.  
  75.  
  76. // quickselect ここから
  77.  
  78. #define SEED32 2463534242
  79.  
  80. static uint32_t y = SEED32;
  81.  
  82. inline void seed32(uint32_t seed) {
  83. y = seed;
  84. }
  85.  
  86. inline static uint32_t rand32() {
  87. y = y ^ (y << 13);
  88. y = y ^ (y >> 17);
  89. return y = y ^ (y << 15);
  90. }
  91.  
  92. inline static val_t* partition(val_t* p_left_, val_t* p_right_) {
  93.  
  94. val_t* const p_pivot = p_left_ + (rand32() % (p_right_ - p_left_));
  95. val_t tmp;
  96. tmp = *p_pivot; *p_pivot = *p_right_; *p_right_ = tmp;
  97.  
  98. val_t const* const p_prev_right = p_right_ - 1;
  99.  
  100. for (val_t* p = p_left_; p < p_prev_right; ++p) {
  101. if (*p > *p_right_) {
  102. tmp = *p; *p = *p_left_; *p_left_ = tmp;
  103. ++p_left_;
  104. }
  105. }
  106. tmp = *p_right_; *p_right_ = *p_left_; *p_left_ = tmp;
  107. return p_left_;
  108. }
  109.  
  110. inline static void quickselect(val_t* p_left_, val_t* p_right_, val_t* k_) {
  111.  
  112. while (p_left_ < p_right_) {
  113. val_t* p_pivot = partition(p_left_, p_right_);
  114. if (p_pivot > k_) {
  115. p_right_ = p_pivot - 1;
  116. } else if (p_pivot < k_) {
  117. p_left_ = p_pivot + 1;
  118. } else {
  119. break;
  120. }
  121. }
  122. }
  123.  
  124. // quickselect ここまで
  125.  
  126. int main(int argc, char* argv[]) {
  127.  
  128. double s;
  129. double e;
  130. clock_t us;
  131. clock_t ue;
  132. val_t* p1;
  133. val_t* p2;
  134. val_t* top;
  135. size_t u_elems;
  136. size_t u_topss;
  137.  
  138. // 上位3件
  139. u_elems = 0;
  140. u_topss = 0;
  141. fprintf(stderr, "上位3件\n");
  142. fprintf(stderr, "要素数,抽出数,アホ(t),アホ(c),エレガント(t),エレガント(c)\n");
  143. for (int i = 0; i < 100; ++i) {
  144. u_elems += 10000;
  145. u_topss = 3;
  146.  
  147. p1 = alloc_data(u_elems);
  148. p2 = alloc_clone_data(p1, u_elems);
  149. top = (val_t*)malloc(u_topss * sizeof(val_t));
  150.  
  151. s = baka_sec_count();
  152. us = clock();
  153. aho_select(p1, u_elems, top, u_topss);
  154. ue = clock();
  155. e = baka_sec_count();
  156. fprintf(stderr, "%u,%u,%.04lf,%.04lf", u_elems, u_topss, e - s, (double)(ue - us) / CLOCKS_PER_SEC);
  157.  
  158. s = baka_sec_count();
  159. us = clock();
  160. quickselect(p2, p2 + u_elems - 1, p2 + u_topss - 1);
  161. ue = clock();
  162. e = baka_sec_count();
  163. fprintf(stderr, ",%.04lf,%.04lf\n", e - s, (double)(ue - us) / CLOCKS_PER_SEC);
  164.  
  165. free_data(p1);
  166. free_data(p2);
  167. free(top);
  168. }
  169.  
  170. seed64(SEED64);
  171.  
  172. // 上位3%
  173. u_elems = 0;
  174. u_topss = 0;
  175. fprintf(stderr, "上位3%\n");
  176. fprintf(stderr, "要素数,抽出数,アホ(t),アホ(c),エレガント(t),エレガント(c)\n");
  177. for (int i = 0; i < 100; ++i) {
  178. u_elems += 10000;
  179. u_topss += 300;
  180.  
  181. p1 = alloc_data(u_elems);
  182. p2 = alloc_clone_data(p1, u_elems);
  183. top = (val_t*)malloc(u_topss * sizeof(val_t));
  184.  
  185. s = baka_sec_count();
  186. us = clock();
  187. aho_select(p1, u_elems, top, u_topss);
  188. ue = clock();
  189. e = baka_sec_count();
  190. fprintf(stderr, "%u,%u,%.04lf,%.04lf", u_elems, u_topss, e - s, (double)(ue - us) / CLOCKS_PER_SEC);
  191.  
  192. s = baka_sec_count();
  193. us = clock();
  194. quickselect(p2, p2 + u_elems - 1, p2 + u_topss - 1);
  195. ue = clock();
  196. e = baka_sec_count();
  197. fprintf(stderr, ",%.04lf,%.04lf\n", e - s, (double)(ue - us) / CLOCKS_PER_SEC);
  198.  
  199. free_data(p1);
  200. free_data(p2);
  201. free(top);
  202. }
  203.  
  204. return 0;
  205. }
  206.  
Time limit exceeded #stdin #stdout #stderr 5s 17356KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
上位3件
要素数,抽出数,アホ(t),アホ(c),エレガント(t),エレガント(c)
10000,3,0.0000,0.0000,0.0001,0.0001
20000,3,0.0001,0.0001,0.0003,0.0003
30000,3,0.0001,0.0001,0.0005,0.0005
40000,3,0.0001,0.0001,0.0005,0.0005
50000,3,0.0001,0.0001,0.0008,0.0008
60000,3,0.0002,0.0002,0.0007,0.0007
70000,3,0.0002,0.0002,0.0002,0.0002
80000,3,0.0002,0.0002,0.0005,0.0005
90000,3,0.0002,0.0002,0.0011,0.0011
100000,3,0.0003,0.0003,0.0012,0.0012
110000,3,0.0003,0.0003,0.0012,0.0012
120000,3,0.0003,0.0003,0.0008,0.0008
130000,3,0.0003,0.0003,0.0022,0.0022
140000,3,0.0003,0.0003,0.0012,0.0012
150000,3,0.0004,0.0004,0.0007,0.0007
160000,3,0.0004,0.0004,0.0025,0.0025
170000,3,0.0004,0.0004,0.0025,0.0025
180000,3,0.0004,0.0004,0.0004,0.0004
190000,3,0.0004,0.0004,0.0014,0.0014
200000,3,0.0005,0.0005,0.0023,0.0023
210000,3,0.0005,0.0005,0.0027,0.0027
220000,3,0.0005,0.0005,0.0025,0.0025
230000,3,0.0005,0.0005,0.0030,0.0030
240000,3,0.0005,0.0005,0.0014,0.0014
250000,3,0.0005,0.0005,0.0017,0.0017
260000,3,0.0005,0.0005,0.0039,0.0039
270000,3,0.0005,0.0005,0.0018,0.0018
280000,3,0.0005,0.0005,0.0028,0.0028
290000,3,0.0005,0.0005,0.0031,0.0031
300000,3,0.0005,0.0005,0.0021,0.0021
310000,3,0.0006,0.0006,0.0013,0.0013
320000,3,0.0006,0.0006,0.0022,0.0022
330000,3,0.0006,0.0006,0.0025,0.0025
340000,3,0.0006,0.0006,0.0028,0.0028
350000,3,0.0007,0.0007,0.0031,0.0031
360000,3,0.0006,0.0006,0.0026,0.0026
370000,3,0.0006,0.0006,0.0028,0.0028
380000,3,0.0006,0.0006,0.0027,0.0027
390000,3,0.0006,0.0006,0.0017,0.0017
400000,3,0.0006,0.0006,0.0031,0.0031
410000,3,0.0006,0.0006,0.0032,0.0032
420000,3,0.0006,0.0006,0.0025,0.0025
430000,3,0.0006,0.0006,0.0006,0.0006
440000,3,0.0006,0.0006,0.0026,0.0026
450000,3,0.0006,0.0006,0.0020,0.0020
460000,3,0.0006,0.0006,0.0021,0.0021
470000,3,0.0006,0.0006,0.0019,0.0019
480000,3,0.0006,0.0006,0.0033,0.0033
490000,3,0.0006,0.0006,0.0027,0.0027
500000,3,0.0006,0.0006,0.0022,0.0022
510000,3,0.0006,0.0006,0.0023,0.0023
520000,3,0.0007,0.0007,0.0013,0.0013
530000,3,0.0007,0.0007,0.0033,0.0033
540000,3,0.0007,0.0007,0.0025,0.0025
550000,3,0.0012,0.0012,0.0029,0.0029
560000,3,0.0007,0.0007,0.0034,0.0034
570000,3,0.0007,0.0007,0.0040,0.0040
580000,3,0.0007,0.0007,0.0054,0.0053
590000,3,0.0007,0.0007,0.0035,0.0035
600000,3,0.0009,0.0009,0.0042,0.0042
610000,3,0.0009,0.0009,0.0019,0.0019
620000,3,0.0008,0.0008,0.0051,0.0051
630000,3,0.0008,0.0008,0.0032,0.0032
640000,3,0.0008,0.0008,0.0039,0.0039
650000,3,0.0008,0.0008,0.0047,0.0047
660000,3,0.0009,0.0009,0.0012,0.0012
670000,3,0.0009,0.0009,0.0022,0.0022
680000,3,0.0009,0.0009,0.0070,0.0070
690000,3,0.0009,0.0009,0.0065,0.0065
700000,3,0.0009,0.0009,0.0007,0.0007
710000,3,0.0009,0.0009,0.0021,0.0021
720000,3,0.0009,0.0009,0.0032,0.0032
730000,3,0.0009,0.0009,0.0035,0.0034
740000,3,0.0010,0.0010,0.0043,0.0043
750000,3,0.0010,0.0010,0.0034,0.0034
760000,3,0.0010,0.0010,0.0021,0.0021
770000,3,0.0010,0.0010,0.0060,0.0060
780000,3,0.0010,0.0010,0.0020,0.0020
790000,3,0.0010,0.0010,0.0046,0.0046
800000,3,0.0010,0.0010,0.0021,0.0021
810000,3,0.0010,0.0010,0.0061,0.0061
820000,3,0.0010,0.0010,0.0044,0.0044
830000,3,0.0010,0.0010,0.0053,0.0053
840000,3,0.0011,0.0011,0.0007,0.0007
850000,3,0.0010,0.0010,0.0059,0.0059
860000,3,0.0011,0.0011,0.0038,0.0038
870000,3,0.0011,0.0011,0.0053,0.0053
880000,3,0.0011,0.0011,0.0024,0.0024
890000,3,0.0011,0.0011,0.0027,0.0027
900000,3,0.0011,0.0011,0.0030,0.0030
910000,3,0.0012,0.0012,0.0077,0.0076
920000,3,0.0012,0.0012,0.0060,0.0060
930000,3,0.0012,0.0011,0.0040,0.0039
940000,3,0.0012,0.0012,0.0049,0.0049
950000,3,0.0012,0.0012,0.0053,0.0053
960000,3,0.0012,0.0012,0.0042,0.0042
970000,3,0.0012,0.0012,0.0030,0.0030
980000,3,0.0012,0.0012,0.0014,0.0014
990000,3,0.0013,0.0013,0.0078,0.0078
1000000,3,0.0012,0.0012,0.0022,0.0022
上位3%
要素数,抽出数,アホ(t),アホ(c),エレガント(t),エレガント(c)
10000,300,0.0016,0.0016,0.0000,0.0000
20000,600,0.0062,0.0062,0.0001,0.0001
30000,900,0.0137,0.0137,0.0001,0.0001
40000,1200,0.0243,0.0243,0.0002,0.0002
50000,1500,0.0377,0.0377,0.0002,0.0002
60000,1800,0.0546,0.0546,0.0003,0.0003
70000,2100,0.0737,0.0737,0.0003,0.0003
80000,2400,0.0966,0.0966,0.0005,0.0005
90000,2700,0.1218,0.1218,0.0003,0.0003
100000,3000,0.1507,0.1507,0.0004,0.0004
110000,3300,0.1832,0.1832,0.0004,0.0004
120000,3600,0.2188,0.2187,0.0003,0.0003
130000,3900,0.2592,0.2592,0.0008,0.0008
140000,4200,0.3012,0.3012,0.0008,0.0008
150000,4500,0.3467,0.3466,0.0007,0.0007
160000,4800,0.4023,0.4020,0.0008,0.0008
170000,5100,0.4445,0.4444,0.0011,0.0011
180000,5400,0.5016,0.5017,0.0015,0.0015
190000,5700,0.5550,0.5550,0.0015,0.0015