(思维)2479 小b分糖果

2479 小b分糖果

  1. 2 秒
  2.  
  3. 262,144 KB
  4.  
  5. 20 分
  6.  
  7. 3 级题

小b想给幼儿园的孩子们分发糖果,有 N 个孩子排成了一排,小b已经根据每个孩子的表现给他们打分。

你需要按照以下要求,帮助小b给这些孩子分发糖果:

首先,每个孩子至少分配到 1 个糖果;

其次,相邻的孩子中,如果评分不同,则评分高的孩子必须获得更多的糖果。

请问小b至少需要准备多少颗糖果呢?

 收起

输入

第一行一个整数N,表示孩子数,其中0<N≤50000;
第二行N个数表示不同孩子的评分,以空格隔开,每个数不超过50000。

输出

输出一个数,表示最多所需的糖果数

输入样例

3
1 2 2

输出样例

4
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
struct fun{
    int x,h;
};
struct cmp{
    bool operator()(fun &a,fun &b)const{
        return a.x>b.x;
    }
};
priority_queue<fun,vector<fun>,cmp>qq;
int a[N],b[N];
int main(){
    int n;
    scanf("%d",&n);
    int p=1;
    a[0]=inf,a[n+1]=inf;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        qq.push({a[i],i});
    }
    fill(b,b+n+1,inf);
    while(!qq.empty()){
        fun t=qq.top();
        qq.pop();
        if(a[t.h-1]>=t.x&&a[t.h+1]>=t.x)
            b[t.h]=1;
        else if(a[t.h-1]>=t.x&&a[t.h+1]<t.x)
            b[t.h]=b[t.h+1]+1;
        else if(a[t.h-1]<t.x&&a[t.h+1]>=t.x)
            b[t.h]=b[t.h-1]+1;
        else if(a[t.h-1]<t.x&&a[t.h+1]<t.x)
            b[t.h]=max(b[t.h-1]+1,b[t.h+1]+1);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
        ans+=b[i];
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/89060912