HDU 4550 Card Game greedy

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4550

Test instructions: There is a card between n (n <= 100) 0~9, from left to right, the card is placed to the far left or right of the previous card, so that the last obtained value is the least, and there is no leading 0;

Input

4

0101

2342001

9876105432

9876543210

Output

1001

1002234

1678905432

1234567890

Idea: If there is no 0, then directly with the double pointer, simulation before and after the insertion value can be, but because of the existence of 0, so need to find the smallest non-0, so that the first to start with MN, and then can be filled Several 0. The so-called can be filled in refers to the last MN filled before the 0, which requires the number of MN, each time the MN is inserted,--cnt; when already filled in front of the 0 o'clock, the last MN when the final entry to the minimum value, or is not filled in 0 o'clock, but only one mn when filled with MN, After that, only the last value is added;

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 Ll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void  out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}#defineN 222intf[n],len,mn,l,r,flag,cnt;CharS[n];voidSolveinta) {    if(flag) {F[r++] =A; return ; }    if(a <= f[l]) {//I didn't fill in 0, but Mn ran out.F[--L] =A; if(A = = mn &&--cnt = =0) flag =1; }    Else if(A = = mn && cnt--= =1){//The front is 0, but only one mn is left, filling inFlag =1, f[--l] =A; }    Elsef[r++] =A;}intMain () {intT;    Read1 (T);  while(t--) {gets (s); Len= strlen (s), MN =Ten; L= R = -; F[l] =Ten; cnt =0; Rep0 (i,0, Len)if(S[i]! ='0')            if(Mn > s[i]-'0') MN = s[i]-'0', cnt =1; Else if(MN = = S[i]-'0') cnt++; Flag=0; Rep0 (i,0, Len) solve (S[i]-'0'); Rep0 (i,l,r) Putchar ('0'+F[i]); Puts (""); }    return 0;}

HDU 4550 Card Game greedy

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.