zoukankan      html  css  js  c++  java
  • BJFU 1549 ——Candy——————【想法题】

    Candy

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

    描述

     

    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

    1 3 5 3 6

    样例输出

    15
    9

    题目来源

    BJFUACM

     

     

    题目大意:给你n个数字的序列,表示n个人的val值。现在给n个人发糖,每人至少发一个,如果某个人的val比旁边的人大,那么那个人必须获得更多的糖果。问你满足条件的情况下,最少要发出多少糖果。   如果某个人val比旁边的两个人都大,那么他应该获得比两旁所得糖果最大值还要多。

    解题思路:要保证某人比两边的最大糖果数大。那么我们从左边扫一遍递增,从右边扫一遍递增。那么该位置所得糖果数,应该是两次当中的最大值。

     

     

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    #include<string>
    #include<iostream>
    #include<queue>
    #include<vector>
    #include<set>
    using namespace std;
    typedef long long LL;
    #define mid (L+R)/2
    #define lson rt*2,L,mid
    #define rson rt*2+1,mid+1,R
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e3 + 300;
    int a[maxn],dp1[maxn],dp2[maxn];
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            for(int i = 1; i <= n; i++){
                scanf("%d",&a[i]);
            }
            memset(dp1,0,sizeof(dp1));
            memset(dp2,0,sizeof(dp2));
            for(int i = 1; i <= n; i++){
                if(a[i] > a[i-1]){
                    dp1[i] = dp1[i-1]+1;
                }else{
                    dp1[i] = 1;
                }
            }
            for(int i = n; i >= 1; i--){
                if(a[i] > a[i+1]){
                    dp2[i] = dp2[i+1] + 1;
                }else{
                    dp2[i] = 1;
                }
            }
            int sum = 0;
            for(int i = 1; i <= n; i++){
                sum += max(dp1[i],dp2[i]);
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
    

     

      

     

  • 相关阅读:
    Linux-第一天
    Hadoop学习10--常用命令记录帖
    C# asp.net repeater实现排序功能,自动排序,点击头部排序,点击列排序
    图片与字符之间的转换
    兼容浏览器 div固定浏览器窗口底部 浮动div
    解决QQ未启用状态,QQ留言图标未启用
    C#Cookie操作类,删除Cookie,给Cookie赋值
    vs2008bin下Debug bll Release文件 obj下的Debug bll Release文件区别
    asp.net限制了上传文件大小为..M,解决方法
    多文件上传ajax jquery
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5436002.html
Copyright © 2011-2022 走看看