fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Comma {
  7. int num, pos;
  8. Comma() {
  9. num = 0;
  10. pos = 0;
  11. }
  12. };
  13.  
  14. int main() {
  15. int n, r;
  16. while(cin >> n >> r) {
  17. int p, c;
  18. string s;
  19. // Filling the array with cards' numbers separated by comma
  20. for (int i = n; i > 0; i--) s += (i == 1 ? to_string(1) : to_string(i) + ",");
  21. if (n > 0) {
  22. for (int i = 0; i < r; i++) {
  23. cin >> p >> c;
  24. // Create object with number of comma in string and its position
  25. Comma comma1, comma2;
  26. int s_len = s.length();
  27. if (p != 1) {
  28. // -------- Looking for indexes of needed commas to pick 'c' cards
  29. for (int j = 0; j < s_len; j++) {
  30. if (comma1.num == p-1) {
  31. comma1.pos = j;
  32. comma1.num = 0;
  33. }
  34. if (comma2.num == p+c-1) {
  35. comma2.pos = j;
  36. comma2.num = 0;
  37. }
  38. if (comma1.pos == 0 && s[j] == ',') comma1.num += 1;
  39. if (comma2.pos == 0 && s[j] == ',') comma2.num += 1;
  40. }
  41. // ---------------------------------------------------------------
  42.  
  43.  
  44. // --------- Swapping cards ---------
  45. string s1 = s.substr(comma1.pos, comma2.pos - comma1.pos);
  46. s.erase(comma1.pos, comma2.pos - comma1.pos);
  47. if (s1[s1.length()-1] != ',') s = s1 + ',' + s;
  48. else s = s1 + s;
  49. // ----------------------------------
  50. }
  51. }
  52. int k = 0;
  53. while (s[k] != ',') k++;
  54. string res = s.substr(0, k);
  55. cout << res << "\n";
  56. }
  57. }
  58. }
Success #stdin #stdout 0s 4628KB
stdin
5 2
3 1
3 1
10 3
1 10
10 1
8 3
0 0
stdout
4
4