派分糖果

有 N 个孩子站成一排,每个孩子有一个分值。给这些孩子派发糖果,需要满足如下需求:
1、每个孩子至少分到一个糖果
2、分值更高的孩子比他相邻位的孩子获得更多的糖果
求至少需要分发多少糖果?

  • 输入描述:
    0,1,0
    
  • 输出描述:
    4
    
  • 输入示例:
    5,4,1,1
    
  • 输出示例:
    7
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n = 5;
    int sugers[n];
    sugers[0] = 1;
    for(int i = 1; i < n; i++){
        if(nums[i] == nums[i-1])
            sugers[i] = sugers[i-1];
        else if(nums[i] > nums[i-1])
            sugers[i] = sugers[i-1] + 1;
        else{
            sugers[i] = 1;
            int now = i;
            int pre = now-1;
            while(pre >= 0&&nums[pre] >= nums[now]&&sugers[pre]<=sugers[now]){
                ++sugers[pre];
                --now;
                --pre;
            }
        }
        int res = 0;
        for(int i=0;i<n;i++){
            res+=sugers[i];
    }
    printf("%d\n",res);
}

 

posted @ 2019-03-03 09:42  strawqqhat  阅读(102)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }