fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 1000;
  5. int main() {
  6. int n, v[MAX_SIZE + 1];
  7. cin >> n;
  8. int zone = 0;
  9.  
  10. for (int i =1; i <= n; ++i) {
  11. cin >> v[i];
  12. }
  13. int destination = 1;
  14. for (int i = 1; i <= n; ++i) {
  15. int flag = 0;
  16.  
  17. for (int j = destination; j <= destination; ++j) {
  18. // cout << i << " ";
  19. if(i != v[j] && v[j] != 0 ) {
  20. flag = 1;
  21. destination = v[j];
  22. /// v[j] = 0;
  23. //cout << destination << " ";
  24. }
  25. if(flag == 0 && i == v[j]) {
  26. // cout << v[j]<< " ";
  27. ++zone;
  28. // v[i] = 0;
  29. // v[j] = 0;
  30. j = n;
  31.  
  32. }
  33. flag = 0;
  34. }
  35. }
  36. cout << zone ;
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5276KB
stdin
4
1 2 3 4 
stdout
1