{"trustable":true,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n div.illustration {\n float: right;\n padding-left: 20px;\n }\n div.illustration .illustration {\n width: 100%;\n border-radius: 4px;\n }\n pre {\n display: block;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n color: #333;\n word-break: break-all;\n word-wrap: break-word;\n }\n div.sampleinteractionread {\n width: 60%;\n margin: 3px 0px 3px 0px;\n }\n div.sampleinteractionread pre {\n margin: 1px 5px 1px 5px;\n border-radius: 5px;\n border: solid 1px rgba(255, 255, 255, 0.25);\n background-color: #cccccc;\n padding: 14px 13px;\n font-family: Courier, monospace;\n font-variant-ligatures: none;\n }\n div.sampleinteractionwrite {\n width: 60%;\n margin: 3px 0px 3px 0px;\n margin-left: auto;\n }\n div.sampleinteractionwrite pre {\n margin: 1px 5px 1px 5px;\n border-radius: 5px;\n border: solid 1px rgba(255, 255, 255, 0.25);\n background-color: #cccccc;\n padding: 14px 13px;\n font-family: Courier, monospace;\n font-variant-ligatures: none;\n }\n table.sample {\n width: 100%;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\n \u003cdiv style\u003d\"width:42.00%\" class\u003d\"illustration\"\u003e\n \u003cimg src\u003d\"CDN_BASE_URL/77af81cf5bd7690e6da54983ff0b7e65?v\u003d1726147645\" alt\u003d\"/problems/cardhand/file/statement/en/img-0001.jpg\" class\u003d\"illustration\"\u003e\n\n \n \u003c/div\u003eWhen dealt cards in the card game Plump it is a good idea\n to start by sorting the cards in hand by suit and rank. The\n different suits should be grouped and the ranks should be\n sorted within each suit. But the order of the suits does not\n matter and within each suit, the cards may be sorted in either\n ascending or descending order on rank. It is allowed for some\n suits to be sorted in ascending order and others in descending\n order.\n\n \u003cp\u003eSorting is done by moving one card at a time from its\n current position to a new position in the hand, at the start,\n end, or in between two adjacent cards. What is the smallest\n number of moves required to sort a given hand of cards?\u003c/p\u003e\n\n \u003ch2\u003eInput\u003c/h2\u003e\n\n \u003cp\u003eThe first line of input contains an integer \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e (\u003cspan class\u003d\"tex2jax_process\"\u003e$1\n \\le n \\le 52$\u003c/span\u003e), the number of cards in the hand. The\n second line contains \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e\n pairwise distinct space-separated cards, each represented by\n two characters. The first character of a card represents the\n rank and is either a digit from \u003ctt class\u003d\"ttfamily\"\u003e2\u003c/tt\u003e to\n \u003ctt class\u003d\"ttfamily\"\u003e9\u003c/tt\u003e or one of the letters \u003ctt class\u003d\"ttfamily\"\u003eT\u003c/tt\u003e, \u003ctt class\u003d\"ttfamily\"\u003eJ\u003c/tt\u003e, \u003ctt class\u003d\"ttfamily\"\u003eQ\u003c/tt\u003e, \u003ctt class\u003d\"ttfamily\"\u003eK\u003c/tt\u003e, and \u003ctt class\u003d\"ttfamily\"\u003eA\u003c/tt\u003e representing Ten, Jack, Queen, King and Ace,\n respectively, given here in increasing order. The second\n character of a card is from the set {\u003ctt class\u003d\"ttfamily\"\u003es\u003c/tt\u003e, \u003ctt class\u003d\"ttfamily\"\u003eh\u003c/tt\u003e, \u003ctt class\u003d\"ttfamily\"\u003ed\u003c/tt\u003e, \u003ctt class\u003d\"ttfamily\"\u003ec\u003c/tt\u003e} representing\n the suits spades\u0026nbsp;\u003cspan class\u003d\"tex2jax_process\"\u003e$\\spadesuit\n $\u003c/span\u003e, hearts\u0026nbsp;\u003cspan class\u003d\"tex2jax_process\"\u003e$\\heartsuit\n $\u003c/span\u003e, diamonds\u0026nbsp;\u003cspan class\u003d\"tex2jax_process\"\u003e$\\diamondsuit $\u003c/span\u003e, and\n clubs\u0026nbsp;\u003cspan class\u003d\"tex2jax_process\"\u003e$\\clubsuit\n $\u003c/span\u003e.\u003c/p\u003e\n\n \u003ch2\u003eOutput\u003c/h2\u003e\n\n \u003cp\u003eOutput the minimum number of card moves required to sort the\n hand as described above.\u003c/p\u003e\n\n \u003ch2\u003eSample 1\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\n2h Th 8c Qh\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e1\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n\n \u003ch2\u003eSample 2\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e7\n9d As 2s Qd 2c Jd 8h\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n\n \u003ch2\u003eSample 3\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\n2h 3h 9c 8c\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e0\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n "}}]}