C.Candy

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

Then there are NN students rating values, 1 leq N leq 300, 1 leq values leq 100001N300,1values10000.

Output:

The minimum number of candies you must give.

样例1

输入:

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

输出:

15
9

贪心。调了半天。扫两遍

/* ***********************************************
Created Time  :2016/4/24 17:15:25
File Name     :1.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
int a[maxn];
int b[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++)cin>>a[i];
        int tmp=1;
        cle(b);
        for(int i=1;i<n;i++){
            if(a[i]>a[i-1])b[i]=max(tmp++,b[i]);
            else tmp=1;
        }    
        tmp=1;
        for(int i=n-2;i>=0;i--){
            if(a[i]>a[i+1])b[i]=max(tmp++,b[i]);
            else tmp=1;
        }
        int sum=0;
        for(int i=0;i<=n;i++)sum+=b[i];
        cout<<sum+n<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/pk28/p/5427537.html