Ultraviolet A Problem Solution: 120-stacks of flapjacks

Source: Internet
Author: User

I just simulate the flipping. we are not required to find the optimal solution, so I choose a way easy to understand. I find the biggest cake in the unsorted stack, and then check its position. if it is at the bottom of the stack, then nothing need to be done. otherwise, at most two steps are needed to flip it to the bottom of the unsorted Stack: one flips it to the top, the other flips it to the bottom. repeat until the unsorted stack shrinks to none.

Code:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 120 C ++ "stacks of flapjacks "*/
  6. # Include <algorithm>
  7. # Include <deque>
  8. # Include <fstream>
  9. # Include <iostream>
  10. # Include <iterator>
  11. # Include <list>
  12. # Include <map>
  13. # Include <queue>
  14. # Include <set>
  15. # Include <sstream>
  16. # Include <stack>
  17. # Include <string>
  18. # Include <vector>
  19. Using namespace STD;
  20. Int const cakecount = 31;
  21. Int flip_sort (int * cakes, int ncakes, int flips [])
  22. {
  23. Int sorted [cakecount];
  24. Copy (cakes, cakes + ncakes, sorted );
  25. Sort (sorted, sorted + ncakes );
  26. Int nflips = 0;
  27. For (INT I = ncakes-1; I> = 0; -- I ){
  28. Int P = I;
  29. For (; P> = 0 & cakes [p]! = Sorted [I]; -- p ){}
  30. If (P = I) continue;
  31. If (P! = 0 ){
  32. Flips [nflips ++] = ncakes-P;
  33. For (Int J = 0, K = P; j <K; ++ J, -- K)
  34. Cakes [J] ^ = cakes [K], cakes [k] ^ = cakes [J], cakes [J] ^ = cakes [k];
  35. }
  36. Flips [nflips ++] = ncakes-I;
  37. For (Int J = 0, K = I; j <K; ++ J, -- K)
  38. Cakes [J] ^ = cakes [K], cakes [k] ^ = cakes [J], cakes [J] ^ = cakes [k];
  39. }
  40. Flips [nflips ++] = 0;
  41. Return nflips;
  42. }
  43. Int main (INT argc, char * argv [])
  44. {
  45. # Ifndef online_judge
  46. Filebuf In, out;
  47. Cin. rdbuf (in. Open (string (argv [0]) + ". In"). c_str (), ios_base: In ));
  48. Cout. rdbuf (out. Open (string (argv [0]) + ". Out"). c_str (), ios_base: Out ));
  49. # Endif
  50. String line;
  51. While (Getline (CIN, line )){
  52. Stringstream SS (line );
  53. Int cakes [cakecount];
  54. Int ncakes = 0;
  55. For (; SS> cakes [ncakes]; ++ ncakes ){}
  56. For (INT I = 0; I <ncakes; ++ I)
  57. Cout <(I> 0? "": "") <Cakes [I];
  58. Cout <'/N ';
  59. Int flips [cakecount];
  60. Int nflips = flip_sort (cakes, ncakes, flips );
  61. For (INT I = 0; I <nflips; ++ I)
  62. Cout <(I> 0? "": "") <Flips [I];
  63. Cout <'/N ';
  64. }
  65. Return 0;
  66. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.