I saw this question on csdn and shared it with friends. If there are other practices, I also hope to share them with others.
Question details
There are n Children standing in a row (numbers from 0 to n-1). Each child has a rating value, which is stored in the ratings array. The teacher needs to score them
With candy, each child needs at least one candy. For any two adjacent children, I and I + 1, the rating value must be smaller than the rating value.
There are many matches of candy (there is no need to allocate the same number of sweets for the same rating ).
Calculate the minimum number of sweets required to complete the allocation.
Input Format:
Multiple groups of data. The first row of each group is a positive integer n.
In the next n rows, each row has a positive integer, indicating the rating value of each child. All integers cannot exceed 100000.
Output Format:
A row of data in each group, including a positive integer, indicates the number of less required sweets.
Next solution:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace allocates candy {class program {static void main (string [] ARGs) {run ();} static int converttoint (string Str) {try {int number = convert. toint32 (STR); If (number> 100000) {console. writeline ("the number entered exceeds the requirement! "); Message ();} return number;} catch {console. writeline (" incorrect input character format! "); Message ();} return-1;} static void run () {console. clear (); console. write ("Enter the number of children:"); int n = converttoint (console. readline (); int [] ratings = new int [N]; for (INT I = 0; I <n; I ++) {console. write (string. format ("Enter rating of {0} children:", I + 1); ratings [I] = converttoint (console. readline ();} If (n <0) {console. writeline ("the number you entered does not match the actual situation! ");} Else if (n = 0) {console. writeline (string. format ("{0} candy 1", 0);} else if (n = 1) {console. writeline (string. format ("{0} Candy 2", 1);} else {int [] candys = new int [N]; for (INT I = 0; I <n; I ++) {dictionary <int, int> DIC = new dictionary <int, int> (); For (Int J = 0; j <N; j ++) {If (candys [J] = 0) {If (DIC. count () <= 0) {dic. add (J, ratings [J]);} else if (ratings [J] = dic. values. first () {dic. add (J, ratings [J]);} else if (ratings [J] <dic. values. first () {dic. clear (); dic. add (J, ratings [J]) ;}} if (DIC. count ()> 0) {foreach (var d in DIC) {If (D. key = 0) {candys [D. key] = candys [D. key + 1] + 1;} else if (D. key = n-1) {If (D. value = ratings [D. key-1]) {candys [D. key] = 1;} else {candys [D. key] = candys [D. key-1] + 1 ;}} else {If (candys [D. key-1]> candys [D. key + 1]) {If (D. value = ratings [D. key-1]) {candys [D. key] = candys [D. key + 1] + 1;} else {candys [D. key] = candys [D. key-1] + 1 ;}} else {If (D. value = ratings [D. key + 1]) {candys [D. key] = candys [D. key-1] + 1;} else {candys [D. key] = candys [D. key + 1] + 1 ;}}}} else {break ;}} console. writeline (); console. writeline (string. format ("{0} candy 3 required", candys. sum ();} message ();} static void message () {console. write ("Enter 1 to play the game again, enter any other key to exit:"); string key = console. readline (); If (Key = "1") {run ();} else {environment. exit (0 );}}}}
Happy Summer online programming competition question 4: allocating candy