120


SUBMITTED BY: Guest

DATE: Dec. 25, 2013, 3:05 p.m.

FORMAT: Text only

SIZE: 1.5 kB

HITS: 2110

  1. #include <cstdio>
  2. #include <cstdlib>
  3. int stack[30];
  4. int stackCnt;
  5. int sorted[30];
  6. int compare (const void * a, const void * b)
  7. {
  8. return ( *(int*)a - *(int*)b );
  9. }
  10. void flip(int place)
  11. {
  12. int tmp;
  13. for (int i = 0; i <= place/2; ++i)
  14. {
  15. tmp = stack[i];
  16. stack[i] = stack[place-i];
  17. stack[place-i] = tmp;
  18. }
  19. }
  20. int main()
  21. {
  22. int pancake;
  23. char c;
  24. while (true)
  25. {
  26. stackCnt = 0;
  27. c = 0;
  28. while (scanf("%d%c", &pancake, &c) == 2 && c != '\n')
  29. {
  30. stack[stackCnt++] = pancake;
  31. printf("%d ", pancake);
  32. }
  33. if (c == '\n')
  34. {
  35. stack[stackCnt++] = pancake;
  36. printf("%d\n", pancake);
  37. }
  38. else
  39. break;
  40. // copy stack
  41. for (int i = 0; i < stackCnt; ++i)
  42. sorted[i] = stack[i];
  43. qsort(sorted, stackCnt, sizeof(int), compare);
  44. // najit umisteni aktualne nejvetsi palacinky
  45. // 1. pokud je vespod pokracuj
  46. // 2. pokud je na vrchu, prehod ji na misto
  47. // 3. jinak ji prehod na vrsek a pokracuj bodem 2
  48. for (int i = stackCnt - 1; i; --i)
  49. {
  50. if (sorted[i] == stack[i])
  51. continue;
  52. else if (sorted[i] == stack[0])
  53. {
  54. printf("%d ", stackCnt - i);
  55. flip(i);
  56. }
  57. else
  58. {
  59. int j;
  60. for (j = 0; stack[j] != sorted[i]; ++j);
  61. printf("%d ", stackCnt - j);
  62. flip(j);
  63. ++i;
  64. }
  65. }
  66. printf("0\n");
  67. }
  68. return 0;
  69. }

comments powered by Disqus