第二届北京林业大学“计蒜客”杯程序设计竞赛 C题 Candy

题目链接


Candy

时间限制(C/C++):1000MS/3000MS          运行内存限制:65536KByte
总提交:21            测试通过:11

描述

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?


输入

The input consists of multiple test cases.

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

Then there are N students rating values, 1 <= N <= 300, 1 <= values <= 10000.


输出

The minimum number of candies you must give.

样例输入

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

样例输出

15
9

题目来源

BJFUACM



从前后两边找递增  


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[305];
int dp1[305],dp2[305];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        dp1[0]=1;
        for(int i=1;i<n;i++)
        {
            if(a[i]>a[i-1])
            {
                dp1[i]=dp1[i-1]+1;
            }
            else
            {
                dp1[i]=1;
            }
        }
        dp2[n-1]=1;
        for(int i=n-2;i>=0;i--)
        {
            if(a[i]>a[i+1])
            {
                dp2[i]=dp2[i+1]+1;
            }
            else
            {
                dp2[i]=1;
            }
        }
        int ans=0;
        for(int i=0;i<n;i++)
        {
           // printf("%d %d\n",dp1[i],dp2[i]);
            ans+=max(dp1[i],dp2[i]);
        }
        printf("%d\n",ans);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值