fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. int n, m;
  10. cin >> n >> m;
  11.  
  12. list<int> queue;
  13. for (int i = 1; i <= n; ++i) {
  14. queue.push_back(i);
  15. }
  16.  
  17. vector<list<int>::iterator> pos(n + 1);
  18. for (auto it = queue.begin(); it != queue.end(); ++it) {
  19. pos[*it] = it;
  20. }
  21.  
  22. for (int i = 0; i < m; ++i) {
  23. int a;
  24. cin >> a;
  25. queue.splice(queue.begin(), queue, pos[a]);
  26. pos[a] = queue.begin();
  27. }
  28.  
  29. for (int student : queue) {
  30. cout << student << " ";
  31. }
  32. cout << endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5260KB
stdin
5 2
5 4
stdout
4 5 1 2 3