Advertisement
VladNitu

biObjTree

Apr 23rd, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. //
  2. // Created by Vlad Nitu on 16.01.2023.
  3. //
  4.  
  5. #include <bits/stdc++.h>
  6. #include "Files.h"
  7.  
  8. using namespace std;
  9.  
  10.  
  11. #define MAX_DEPTH 5
  12. #define FMAX 501
  13. #define ll long long
  14. #define mkp make_pair
  15. #define mkt make_tuple
  16. #define lsb(x) (x & -x)
  17.  
  18. void __print(int x) { cerr << x; }
  19.  
  20. void __print(long x) { cerr << x; }
  21.  
  22. void __print(long long x) { cerr << x; }
  23.  
  24. void __print(unsigned x) { cerr << x; }
  25.  
  26. void __print(unsigned long x) { cerr << x; }
  27.  
  28. void __print(unsigned long long x) { cerr << x; }
  29.  
  30. void __print(float x) { cerr << x; }
  31.  
  32. void __print(double x) { cerr << x; }
  33.  
  34. void __print(long double x) { cerr << x; }
  35.  
  36. void __print(char x) { cerr << '\'' << x << '\''; }
  37.  
  38. void __print(const char *x) { cerr << '\"' << x << '\"'; }
  39.  
  40. void __print(const string &x) { cerr << '\"' << x << '\"'; }
  41.  
  42. void __print(bool x) { cerr << (x ? "true" : "false"); }
  43.  
  44. template<typename T, typename V, typename W>
  45. void __print(const std::tuple<T, V, W> &x) {
  46. cerr << '{';
  47. __print(std::get<0>(x));
  48. cerr << ',';
  49. __print(std::get<1>(x));
  50. cerr << ',';
  51. __print(std::get<2>(x));
  52. cerr << '}';
  53. }
  54.  
  55. template<typename T, typename V>
  56. void __print(const pair<T, V> &x) {
  57. cerr << '{';
  58. __print(x.first);
  59. cerr << ',';
  60. __print(x.second);
  61. cerr << '}';
  62. }
  63.  
  64. template<typename T>
  65. void __print(const T &x) {
  66. int f = 0;
  67. cerr << '{';
  68. for (auto &i: x) cerr << (f++ ? "," : ""), __print(i);
  69. cerr << "}";
  70. }
  71.  
  72. void _print() { cerr << "]\n"; }
  73.  
  74. template<typename T, typename... V>
  75. void _print(T t, V... v) {
  76. __print(t);
  77. if (sizeof...(v)) cerr << ", ";
  78. _print(v...);
  79. }
  80.  
  81. #ifndef ONLINE_JUDGE
  82. #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
  83. #else
  84. #define dbg(x...)
  85. #endif
  86.  
  87. int D, label, N = 0, dim = 0, feature;
  88. std::vector<int> labels, xs;
  89. std::vector<std::vector<int>> features;
  90. std::string line;
  91.  
  92. inline size_t hash_vector(const std::vector<int> &vec) { // SO
  93. std::size_t seed = vec.size();
  94. for (auto &i: vec) {
  95. seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  96. }
  97. return seed;
  98. }
  99.  
  100. std::unordered_map<size_t, vector<pair<int, int>>> memo[MAX_DEPTH];
  101. bool feats[FMAX];
  102.  
  103.  
  104. void reduce(vector<pair<int, int>> &v) { // O(v * log(v)), remove duplicates and keep track (by a single pass) of only the non-dominated Pareto fronts
  105.  
  106. sort(v.begin(), v.end());
  107. vector<pair<int, int>> ans{};
  108.  
  109. for (const auto [FP, FN]: v) {
  110. if (ans.empty())
  111. ans.push_back({FP, FN});
  112. else if (FN < ans.back().second)
  113. ans.push_back({FP, FN});
  114. }
  115.  
  116. swap(v, ans);
  117. }
  118.  
  119. inline vector<pair<int, int>> combine(vector<pair<int, int>> &l, const vector<pair<int, int>> &r) {
  120.  
  121. vector<pair<int, int>> ans{};
  122. for (int i = 0; i < l.size(); ++i)
  123. for (int j = 0; j < r.size(); ++j)
  124. ans.push_back({l[i].first + r[j].first, l[i].second + r[j].second});
  125.  
  126. reduce(ans);
  127.  
  128. return ans;
  129. }
  130.  
  131. inline vector<pair<int, int>> solve(int depth, const std::vector<int> &datapoints) {
  132.  
  133. vector<pair<int, int>> ans{};
  134.  
  135. size_t hash_value = hash_vector(datapoints);
  136.  
  137. if (memo[depth].find(hash_value) != memo[depth].end())
  138. return memo[depth][hash_value];
  139.  
  140. if (depth == D - 1) { // D = 1 case
  141.  
  142. for (int split = 0; split < dim; ++split)
  143.  
  144. if (feats[split]) {
  145.  
  146. int a = 0, b = 0, c = 0, d = 0;
  147.  
  148. for (const int &index: datapoints) {
  149. const std::vector<int> &v = features[index];
  150. if (v[split] == 0) {
  151. if (labels[index] == 0)
  152. a++;
  153. else
  154. b++;
  155. } else {
  156. if (labels[index] == 0)
  157. c++;
  158. else
  159. d++;
  160. }
  161. }
  162.  
  163. // cand1 (0, 0) -> (a + c, 0)
  164. // cand2 (0, 1) -> (c, b)
  165. // cand3 (1, 0) -> (a, d)
  166. // cand4 (1, 1) -> (0, b + d)
  167.  
  168. pair<int, int> candidate1{a + c, 0}, candidate2{c, b}, candidate3{a, d}, candidate4{0, b + d};
  169.  
  170.  
  171. ans.push_back(candidate4);
  172. ans.push_back(candidate2);
  173. ans.push_back(candidate3);
  174. ans.push_back(candidate1);
  175.  
  176. reduce(ans);
  177.  
  178. if (!ans.empty() && ans[0].first == 0 && ans[0].second == 0)
  179. break;
  180. }
  181.  
  182. memo[depth][hash_value] = ans;
  183. return ans;
  184. } else {
  185.  
  186. for (int split = 0; split < dim; ++split)
  187. if (feats[split]) {
  188.  
  189. feats[split] = false;
  190.  
  191. std::vector<int> neg{};
  192. std::vector<int> pos{};
  193.  
  194. for (const int &index: datapoints) {
  195. const std::vector<int> &v = features[index];
  196. if (v[split] == 0)
  197. neg.push_back(index);
  198. else
  199. pos.push_back(index);
  200. }
  201.  
  202. vector<pair<int, int>> l{};
  203. vector<pair<int, int>> r{};
  204.  
  205. l = solve(depth + 1, neg);
  206. if (!l.empty())
  207. r = solve(depth + 1, pos);
  208.  
  209. if (!l.empty() && !r.empty()) {
  210. vector<pair<int, int>> combined = combine(l, r);
  211. ans.insert(ans.begin(), combined.begin(), combined.end()); // Remove duplicates
  212. reduce(ans);
  213. }
  214. feats[split] = true;
  215.  
  216. if (!ans.empty() && ans[0].first == 0 && ans[0].second == 0)
  217. break;
  218. }
  219. }
  220.  
  221. memo[depth][hash_value] = ans;
  222. return ans;
  223. }
  224.  
  225. inline std::vector<int> init() {
  226. N = (int) features.size();
  227. dim = (int) features[0].size();
  228.  
  229. vector<int> datapoints(N);
  230. for (int i = 0; i < N; ++i)
  231. datapoints[i] = i;
  232.  
  233. for (int i = 0; i < dim; ++i)
  234. feats[i] = true;
  235.  
  236. return datapoints;
  237. }
  238.  
  239. inline std::vector<int> read() {
  240. std::stringstream stringstream;
  241. std::getline(std::cin, line);
  242. stringstream << line;
  243. stringstream >> D;
  244. stringstream.clear();
  245.  
  246. while (std::getline(std::cin, line)) {
  247. stringstream << line;
  248. stringstream >> label;
  249. labels.push_back(label);
  250.  
  251. xs = std::vector<int>{};
  252. while (stringstream >> feature)
  253. xs.push_back(feature);
  254.  
  255. features.push_back(xs);
  256.  
  257. stringstream.clear();
  258. }
  259.  
  260. return init();
  261. }
  262.  
  263. inline void rwFrom(const char *read_filename, const char *write_filename) {
  264. // Speed-up reading
  265. std::ios::sync_with_stdio(false);
  266. std::cin.tie(nullptr);
  267.  
  268. freopen(read_filename, "r", stdin);
  269. freopen(write_filename, "w", stdout);
  270. }
  271.  
  272. int main() {
  273.  
  274. std::ios::sync_with_stdio(false);
  275. std::cin.tie(nullptr);
  276.  
  277. // std::cout << "We are currently in: " << std::filesystem::current_path() << '\n';
  278.  
  279. auto start = std::chrono::steady_clock::now();
  280.  
  281. rwFrom(Files::anneal_in, Files::anneal_out);
  282. std::vector<int> datapoints = read();
  283.  
  284. vector<pair<int, int>> ans = solve(0, datapoints);
  285.  
  286.  
  287. for (const pair<int, int> &FP_FN: ans)
  288. cout << FP_FN.first << ' ' << FP_FN.second << '\n';
  289.  
  290. // auto end = std::chrono::steady_clock::now();
  291. //
  292. // std::cout << "Elapsed time: "
  293. // << std::chrono::duration_cast<std::chrono::seconds>(end - start).count()
  294. // << " sec";
  295.  
  296. return 0;
  297. }
  298.  
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement