北京林业大学“计蒜客”杯程序设计竞赛 网络赛 C-Candy【模拟】

There are NN children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

(1) Each child must have at least one candy.

(2) Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

Input:

The input consists of multiple test cases.

The first line of each test case has a number NN, which indicates the number of students.

Then there are NN students rating values, 1 \leq N \leq 300, 1 \leq values \leq 100001N300,1values10000.

Output:

The minimum number of candies you must give.

样例1

输入:

5
1 2 3 4 5
5 
1 3 5 3 6

输出:

15
9

解题思路:

题目大意就是看一个人的身高,如果身高比旁边两个人的大,要保证分的糖果比旁边的人多,问最小糖果数。

#include <cstdio>
#define N 301
int num[N], pro[N];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        for(int i=0; i< n; i++) 
            scanf("%d", &pro[i]);
        num[0]= 1;
        for(int i=1; i<n; i++)
        {
            if(pro[i]> pro[i-1])
                num[i]=num[i-1]+1;
            else
                num[i]=1;
        }
        
        for(int i=n-2; i>=0; i--)
        {
            if(pro[i] >pro[i+1] && num[i] <= num[i+1])
            {
                num[i]= num[i+1]+1;
            }
        }
        
        int sum= 0;
        for(int i=0; i< n; i++)
        {   
			sum += num[i];
        }
        printf("%d\n", sum);
    }
    return 0;
}


你可能感兴趣的:(模拟)