fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7. int N;
  8. cin >> N;
  9. vector<int> soldiers(2 * N);
  10. vector<pair<int, int>> pos(N + 1, {-1, -1});
  11.  
  12. for (int i = 0; i < 2 * N; i++) {
  13. cin >> soldiers[i];
  14. if (pos[soldiers[i]].first == -1) {
  15. pos[soldiers[i]].first = i;
  16. } else {
  17. pos[soldiers[i]].second = i;
  18. }
  19. }
  20.  
  21. long long defenseValue = 0;
  22.  
  23. for (int num = 1; num <= N; num++) {
  24. int left = pos[num].first;
  25. int right = pos[num].second;
  26.  
  27. vector<int> between;
  28. for (int i = left + 1; i < right; i++) {
  29. between.push_back(soldiers[i]);
  30. }
  31.  
  32. sort(between.begin(), between.end());
  33.  
  34. // 計算有多少不同的數字在between中出現至少兩次
  35. int cnt = 0;
  36. for (int i = 0; i < between.size(); ) {
  37. if (between[i] > num) {
  38. int j = i;
  39. while (j < between.size() && between[j] == between[i]) j++;
  40. if (j - i >= 2) cnt++;
  41. i = j;
  42. } else {
  43. i++;
  44. }
  45. }
  46. defenseValue += cnt;
  47. }
  48.  
  49. cout << defenseValue << endl;
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5288KB
stdin
3
1 3 2 3 1 2
stdout
1