Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4550
After thinking for a long time, I finally thought about AC in various categories. If I had no confidence in myself at the scene, I would probably still need wa, and then I would search for a question, I found that all people thought it was a simple question. It seems that I am still too weak, but Mudanjiang did not come up with K. It seems that I am greedy and have problems with thinking.
D is a deque
The simplest algorithm is, if the current number is <= D. front (), then insert the front of the queue, otherwise Insert the back of the queue, but there is zero so it needs to be processed separately, or you have to look for more examples to find the law
My policies:
1. records the number of 0 zero, the minimum number of non-zero CNT
2. For the judgment policy, see the code.
# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # include <deque> # include <vector> # include <queue> using namespace STD; # define in (s) freopen (S, "r", stdin) const int maxn = 100 + 5; char s [maxn]; int A [maxn], Len; void solve () {deque <int> D; int _ min = 1000, zero = 0, CNT = 0; For (INT I = 0; I <Len; I ++) if (A [I]) _ min = min (_ min, a [I]); else zero ++; For (INT I = 0; I <Len; I ++) if (a [I] = _ min) CNT ++; D. push_front (A [0]); // if (a [0] = _ min) CNT --; if (a [0] = 0) zero --; for (INT I = 1; I <Len; I ++) {if (a [I]) {If (D. front () {if (a [I] = _ min) CNT --; if (a [I] <= D. front () d. push_front (A [I]); else D. push_back (A [I]);} else // The first is 0 {If (CNT = 1 & A [I] = _ min) {CNT --; D. push_front (A [I]); continue;} // there is more than one minimum number behind it. If (A [I] = _ min) CNT --; D. push_back (A [I]);} continue;} // If a [I] is 0, zero --; If (CNT) // there is a minimum value D behind it. push_front (A [I]); else D. push_back (A [I]);} For (INT I = 0; I <D. size (); I ++) printf ("% d", d [I]); putchar ('\ n');} int main () {// In ("hdu4550.txt"); int ncase; scanf ("% d", & ncase); While (ncase --) {scanf ("% s", S ); len = strlen (s); For (INT I = 0; I <Len; I ++) A [I] = s [I]-'0 '; solve ();} return 0 ;}
HDU 4550 greedy thinking questions are good