fork(1) download
  1. #include <fstream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. istream& in = cin;
  11. int cars, overtakings;
  12.  
  13. in >> cars >> overtakings;
  14. vector<int> positions( cars );
  15.  
  16. for( int i=0; i < cars; i++ )
  17. {
  18. int carID;
  19. in >> carID;
  20.  
  21. positions[carID-1] = i;
  22. }
  23.  
  24. for( int i=0; i < overtakings; i++ )
  25. {
  26. int firstCar, secondCar;
  27. in >> firstCar >> secondCar;
  28.  
  29. swap( positions[firstCar-1], positions[secondCar-1] );
  30. }
  31.  
  32. vector<int>::iterator found = find(positions.begin(), positions.end(), 0);
  33. int result = distance(positions.begin(), found) + 1;
  34.  
  35. ostream& out = cout;
  36. out << result;
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.02s 2860KB
stdin
3 4
2
1
3
3 1
3 2
1 2
2 1
stdout
3