fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. void print_vec_pair(std::vector<std::pair<int, int>>& v) {
  5.  
  6. for (const auto& a : v) {
  7. std::cout << "(" << a.first << ", " << a.second << ") ";
  8. }
  9. std::cout << std::endl;
  10. }
  11.  
  12. bool compare_pairs(std::pair<int, int>& a, std::pair<int, int>& b) {
  13.  
  14. if (a.second == b.second)
  15. return true;
  16. else
  17. return false;
  18. }
  19.  
  20. std::vector<std::pair<int, int>> decimation(std::vector<std::pair<int, int>>& data, int N) {
  21.  
  22. std::vector<std::pair<int, int>> res;
  23. int cnt = 1;
  24.  
  25. for (size_t i = 0; i < data.size()-1; i++) {
  26.  
  27. if (cnt == 1 || cnt == N) {
  28. res.push_back(data[i]);
  29. if (cnt == N) {
  30. cnt = 1;
  31. }
  32. }
  33.  
  34. if (compare_pairs(data[i], data[i + 1])) {
  35. cnt++;
  36. }
  37. else {
  38. cnt == 1;
  39. }
  40.  
  41. }
  42.  
  43. return res;
  44. }
  45.  
  46.  
  47. int main()
  48. {
  49. std::vector<std::pair<int, int>> data;
  50. data.push_back({ 1, 10 });
  51. data.push_back({ 2, 11 });
  52. data.push_back({ 3, 11 });
  53. data.push_back({ 4, 11 });
  54. data.push_back({ 5, 11 });
  55. data.push_back({ 6, 10 });
  56. data.push_back({ 7, 11 });
  57. data.push_back({ 8, 11 });
  58. data.push_back({ 9, 11 });
  59. data.push_back({ 10, 11 });
  60. data.push_back({ 11, 11 });
  61. data.push_back({ 12, 11 });
  62. data.push_back({ 13, 11 });
  63. data.push_back({ 14, 10 });
  64.  
  65. auto result3 = decimation(data, 3);
  66. auto result4 = decimation(data, 4);
  67.  
  68. print_vec_pair(data);
  69. std::cout << std::endl;
  70. print_vec_pair(result3);
  71. std::cout << std::endl;
  72. print_vec_pair(result4);
  73. }
Success #stdin #stdout 0s 4296KB
stdin
Standard input is empty
stdout
(1, 10) (2, 11) (3, 11) (4, 11) (5, 11) (6, 10) (7, 11) (8, 11) (9, 11) (10, 11) (11, 11) (12, 11) (13, 11) (14, 10) 

(1, 10) (2, 11) (4, 11) (8, 11) (10, 11) (12, 11) 

(1, 10) (2, 11) (5, 11) (6, 10) (7, 11) (10, 11) (13, 11)