Geek programming Exercises--Hair candy problem

Source: Internet
Author: User

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

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.