Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

multibit

  • Loading branch information...
commit 3e09845cbe3ae42bea44e3d51a36fd97205c27a4 1 parent 0210548
Hugo Peixoto authored

Showing 1 changed file with 108 additions and 0 deletions. Show diff stats Hide diff stats

  1. +108 0 multibit.cc
108 multibit.cc
... ... @@ -0,0 +1,108 @@
  1 +#include <math.h>
  2 +#include <stdint.h>
  3 +#include <stdlib.h>
  4 +#include <string.h>
  5 +#include <stdio.h>
  6 +#include <map>
  7 +#include <algorithm>
  8 +
  9 +struct solution {
  10 + uint32_t groups[32];
  11 + uint32_t group_count;
  12 + uint32_t cost;
  13 +};
  14 +
  15 +static void solve (const uint32_t* histogram, const uint32_t a_max_bits, uint32_t a_current_bit, solution* a_current, solution* a_best)
  16 +{
  17 + if (a_current_bit == a_max_bits) {
  18 + if (a_current->cost < a_best->cost) {
  19 + memcpy(a_best, a_current, sizeof(solution));
  20 + }
  21 +
  22 + return;
  23 + }
  24 +
  25 + for (uint32_t group_size = 1; group_size <= a_max_bits - a_current_bit; ++group_size) {
  26 + uint32_t group_cost = 0;
  27 + uint32_t covered = 0;
  28 +
  29 + covered = a_current_bit + group_size;
  30 + for (uint32_t bit = a_current_bit; bit < covered; ++bit) {
  31 + group_cost += histogram[bit];
  32 + }
  33 +
  34 + if (covered == a_max_bits) {
  35 + group_cost *= covered + a_current->group_count;
  36 + } else {
  37 + group_cost *= covered + a_current->group_count + 1;
  38 + }
  39 +
  40 + if (group_cost + a_current->cost < a_best->cost) { // do some pruning
  41 + a_current->cost += group_cost;
  42 + a_current->groups[a_current->group_count] = group_size;
  43 + ++a_current->group_count;
  44 + solve(histogram, a_max_bits, covered, a_current, a_best);
  45 + --a_current->group_count;
  46 + a_current->cost -= group_cost;
  47 + }
  48 + }
  49 +}
  50 +
  51 +uint32_t read (int32_t* values)
  52 +{
  53 + uint32_t ret;
  54 + for (ret = 0; scanf("%d", values + ret) == 1; ++ret);
  55 + return ret;
  56 +}
  57 +
  58 +uint32_t make_bit_histogram (uint32_t nvalues, const int32_t* values, uint32_t* histogram)
  59 +{
  60 + uint32_t max_bits = 0;
  61 +
  62 + memset(histogram, 0, 32*4);
  63 + for (uint32_t i = 0; i < nvalues; ++i) {
  64 + uint32_t entry = abs(values[i]);
  65 + uint32_t bits = floor(log2(entry<1?1:entry)) + 1;
  66 +
  67 + ++histogram[bits - 1];
  68 + if (bits > max_bits) {
  69 + max_bits = bits;
  70 + }
  71 + }
  72 +
  73 + return max_bits;
  74 +}
  75 +
  76 +uint32_t optimize (uint32_t max_bits, const uint32_t* histogram)
  77 +{
  78 + solution initial;
  79 + solution best;
  80 +
  81 + memset(&initial, 0, sizeof(initial));
  82 + memset(&best, 0, sizeof(best));
  83 +
  84 + best.cost = static_cast<uint32_t>(-1);
  85 + solve(histogram, max_bits, 0, &initial, &best);
  86 +
  87 + return best.cost;
  88 +}
  89 +
  90 +int32_t values[100000000]; // 100 M
  91 +uint32_t bit_histogram[32];
  92 +
  93 +uint32_t groups[100000]; // 100 K
  94 +uint32_t bit_histogram2[32];
  95 +
  96 +int main ()
  97 +{
  98 + uint32_t nvalues = read(values);
  99 + uint32_t max_bits = make_bit_histogram(nvalues, values, bit_histogram);
  100 + uint32_t cost = optimize(max_bits, bit_histogram) + nvalues;
  101 +
  102 + printf("%u\n", nvalues);
  103 + printf("%u (%u)\n", cost, cost / 8);
  104 + printf("%lf\n", 1.0 * cost / nvalues);
  105 +
  106 + return 0;
  107 +}
  108 +

0 comments on commit 3e09845

Please sign in to comment.
Something went wrong with that request. Please try again.