Topics
Assuming that n children stand in a row, each child is assigned an integer number. To give the children sugar according to the following requirements.
1. Each child has at least one candy
2, the number of children than the adjacent children to the candy to be more
Ask for at least a few candies to be issued
Input
0 1 3 1 4
Output
9
Analysis
Can cycle two times, the first time from the next, the next two children, the front number is larger than the number of sugar, the second pass from the back, the same as the adjacent children, the latter number than the previous number of the larger than the sugar, and finally add and get results.
Code
#include <iostream>#include <vector>#include <numeric>using namespace STD;intMain () { vector<int>Ratings;inttmp while(Cin>> tmp) Ratings.push_back (TMP);Const intn = ratings.size (); vector<int>Increment (n); for(inti =1, Inc =1; I < n; i++) {if(Ratings[i] > Ratings[i-1]) Increment[i] = max (inc++, Increment[i]);Elseinc =1; } for(inti = n-2, Inc =1; I >=0; i--) {if(Ratings[i] > ratings[i +1]) Increment[i] = max (inc++, Increment[i]);Elseinc =1; }cout<< accumulate (Increment.begin (), Increment.end (), n);return 0;}
Geek programming Exercises--Hair candy problem