Candy and candy ppt

Source: Internet
Author: User

Candy and candy ppt

Description
There are n Children waiting in queue to ask you for candy. Every child has a cute value. You must give each child at least one candy, and the cute and high-value child must have more candy than the adjacent child.
Note: If the cute values of two adjacent persons are the same, there is no limit.
Ask how much candy you have to prepare at least.

For example, there are five children whose cute values are 3, 40, 2, 1, and 10.
Then the minimum solution should be 1, 3, 2, 1, 2, 9 in total
About Input
The first line is a positive integer, n, indicating the number of children. (N <= 100000) There are n positive integers in the second row. The I integer represents the cute value of the I child (within the int range ). Numbers are separated by a space.
About output
A positive integer with the minimum number of sweets to be prepared.
Example Input
53 40 2 1 10
Example output
9


#include <stdio.h>#include <stdlib.h>int main(){int n = 0, j;int *p = 0, *q = 0;int t = 0;scanf("%d", &n);p = (int *)calloc(n, sizeof(int));q = (int *)calloc(n, sizeof(int));for (j = 0; j < n; j++){scanf("%d", &p[j]);}q[0] = 1;for (j = 1; j < n; j++){if (p[j] <= p[j - 1])q[j] = 1;elseq[j] = q[j - 1] + 1;}for (j = n - 2; j >= 0; j--){if (p[j] > p[j + 1] && q[j] <= q[j + 1])q[j] = q[j + 1] + 1;}for (j = 0; j < n; ++j){t += q[j];}printf("\n%d\n", t);return 0;}

Related Article

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.