#include <bits/stdc++.h> using namespace std; /* A map-based data structure [27-bit id+2-bit x] = 29-bit key [16-bit score2+16-bit score2] = 32-bit integer score value */ const int s_max = 65535, x_max = 2, id_max = 7e7; using uint32_scores = vector<uint32_t>; struct double_scores: vector<pair<double,double>> { static double int_to_score(uint32_t x) { assert(x <= s_max); double x1 = x; x1 /= s_max; return x1; } double_scores() {} double_scores(const uint32_scores& s) { for(auto p: s) emplace_back(int_to_score(p&s_max),int_to_score(p>>16)); } }; struct data_tuples: map<uint32_t,uint32_scores> { static uint32_t double_to_int(double s) { assert(s >= 0.0 and s <= 1.0); return round(s *= s_max); } void add_tuple(uint32_t id, uint32_t x, double s1, double s2) { assert(id <= id_max and x <= x_max); uint32_t key = (id << 2)|x; uint32_t value = (double_to_int(s2) << 16) | double_to_int(s1); (*this)[key].push_back(value); } double_scores tuples(uint32_t id, uint32_t x) { assert(id <= id_max and x <= x_max); uint32_t key = (id << 2)|x; auto it = find(key); return it != end() ? double_scores(it->second) : double_scores(); } friend ostream& operator<<(ostream& out, const data_tuples& a) { for(auto p: a) { out << '[' << setw(8) << (p.first>>2) << ',' << (p.first&3) << ']'; for(auto q: double_scores(p.second)) out << '[', out << fixed << setprecision(5) << q.first << ',', out << fixed << setprecision(5) << q.second << ']'; out << '\n'; } return out; } }; int main() { data_tuples a; int n; cin >> n, srand(time(NULL)); for(int i = 0; i < n; ++i) { int id = 1+(rand()%id_max); int x = rand()%(x_max+1); double s1 = double(rand()%(s_max+1))/s_max; double s2 = double(rand()%(s_max+1))/s_max; a.add_tuple(id,x,s1,s2); } cout << a; }
20
[ 3476958,2][0.08582,0.43424] [ 3636082,1][0.24848,0.77708] [ 5929579,1][0.46490,0.22734] [11902905,1][0.59266,0.72465] [16136280,1][0.01297,0.65336] [22288594,1][0.05861,0.51748] [24674473,0][0.00946,0.14098] [35224906,0][0.10130,0.11974] [35289077,1][0.18408,0.67942] [36088458,0][0.65846,0.49737] [37631039,1][0.88070,0.62773] [40889875,1][0.03983,0.99803] [46008668,0][0.29400,0.59052] [49917914,0][0.61711,0.81559] [54605778,1][0.69306,0.23342] [54807037,0][0.66726,0.06534] [63748350,0][0.79892,0.83485] [65318001,2][0.30713,0.06058] [65827659,1][0.61192,0.57873] [68938168,1][0.30338,0.01390]