fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. ll solve(int i, vector<int>& arr, int k, int goalxor, vector<unordered_map<ll, ll>>& g) {
  6. int n = arr.size();
  7. if (i == k) {
  8. if (goalxor == 0)
  9. return 0;
  10. return 1e9;
  11. }
  12.  
  13. int total = (1+(n-i-1)/k);
  14. ll minCost = 1e9;
  15. for (int lastElement = 0; lastElement <= 63; lastElement++) {
  16. int oldxor = goalxor ^ lastElement;
  17. // If g[i][lastElement] doesn't exist, it defaults to 0
  18. minCost = min(minCost, solve(i + 1, arr, k, oldxor, g) + (total - (g[i].count(lastElement) ? g[i][lastElement] : 0)));
  19. }
  20. return minCost;
  21. }
  22.  
  23. int main() {
  24. // Initialize the array and k
  25. vector<int> arr1{1, 2, 3, 4};
  26. int k = 3; // Define k
  27.  
  28. // Create the unordered_map vector
  29. vector<unordered_map<ll, ll>> g(k); // Ensure the vector has size k
  30.  
  31. // Populate the groups
  32. for (int i = 0; i < arr1.size(); i++) {
  33. int groupIndex = i % k;
  34. g[groupIndex][arr1[i]]++; // Grouping the elements
  35. }
  36.  
  37. int goalxor = 0; // Initialize the goal XOR
  38. cout << solve(0, arr1, k, goalxor, g) << endl; // Call solve function and output result
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1