Candy

来源:互联网 发布:暗黑黎明挂机软件 编辑:程序博客网 时间:2024/05/01 04:53

There are N 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 NNN, which indicates the number of students.

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

Output:

The minimum number of candies you must give.

样例1

输入:

51 2 3 4 55 1 3 5 3 6

输出:

159
#include<stdio.h>int a[1000],b[1000];int Max(int a,int b){    return a>b?a:b;}int main(){    int n,i,p,r,j,f;    while(scanf("%d",&n)!=-1)    {        for(i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        b[0]=1;        for(i=1; i<n; )        {            if(a[i]>a[i-1])            {                b[i]=b[i-1]+1;                i++;            }            if(a[i]==a[i-1])            {                b[i]=b[i-1];                i++;            }            if(a[i]<a[i-1])//先找到后面的数小于它前面的数的位置,记住下标            {                int p,val;                p=i-1,val=b[p];                int qq=1;                while(a[i]<=a[i-1]&&i<n) i++;//找到后面的数大于它前面的数的位置,记住下标                b[i-1]=qq;              //  printf("%d %d\n",i-2,p);                for(j=i-2;j>=p;j--)//给p和i-2之间倒着赋值                {                    if(a[j]==a[j-1]) b[j]=qq;                    else b[j]=++qq;                }                b[p]=Max(b[p],val);比较倒着赋值之后的b[p]和同一个位置的val大小,并把b[p]为最大的那一个            }        }        int h=0;        for(i=0; i<n; i++)        {            h+=b[i];//加和        }        printf("%d\n",h);    }}

0 0
原创粉丝点击