fork download
  1. //
  2. // Created by Ayan Ginet on 03.04.2018.
  3. //
  4. #include <iostream>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <map>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. bool myComparison(const pair<long long,pair<long long, long long>> &a,
  15. const pair<long long, pair<long, long>> &b)
  16. {
  17. return a.second.first < b.second.first;
  18. }
  19.  
  20. bool cmp(const pair<long, long> &a,
  21. const pair<long, long> &b)
  22. {
  23. return a.first < b.first;
  24. }
  25.  
  26. vector<int> swap( vector<int> &nums, int first, int second){
  27. if(second >= nums.size()) return nums;
  28. int buf = nums[first];
  29. nums[first] = nums[second];
  30. nums[second] = buf;
  31. return nums;
  32. }
  33.  
  34. int minSwap( vector<int> &nums, int i, int n){
  35. if(i > n) return 0;
  36.  
  37. if(nums[i] == nums[i+1])
  38. return minSwap( nums, i + 2, n);
  39.  
  40. nums = swap(nums, i, i + 1);
  41. int a = 1;
  42. a += minSwap(nums, i + 1, n);
  43.  
  44. nums = swap(nums, i , i + 1);
  45. nums = swap(nums, i + 1 , i + 2);
  46. int b = 1 + minSwap(nums, i + 1, n);
  47.  
  48. return min(a,b);
  49.  
  50. }
  51.  
  52.  
  53.  
  54. int main(){
  55. std::ios::sync_with_stdio(false);
  56. cin.tie(nullptr);
  57.  
  58. int n;
  59. cin >> n;
  60.  
  61. vector<int> nums;
  62.  
  63. for(int i = 0; i < 2 * n; i++){
  64. int buf;
  65. cin >> buf;
  66. nums.push_back(buf);
  67. }
  68.  
  69. cout << minSwap(nums, 0, nums.size());
  70.  
  71. }
Success #stdin #stdout 0s 4348KB
stdin
4

1 1 2 3 3 2 4 4
stdout
2