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\n",sum);
}
return 0;
}

  

BJFU 1549 ——Candy——————【想法题】的更多相关文章

  1. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  2. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  3. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  4. HDU - 5969 最大的位或 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...

  5. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  6. CodeForces - 156B Suspects 逻辑 线性 想法 题

    题意:有1~N,n(1e5)个嫌疑人,有m个人说真话,每个人的陈述都形如X是凶手,或X不是凶手.现在给出n,m及n个陈述(以+x/-X表示)要求输出每个人说的话是true ,false or notd ...

  7. 2016华中农业大学预赛 E 想法题

    Problem E: Balance Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 205  Solved: 64[Submit][Status][We ...

  8. codeforces 657C - Bear and Contribution [想法题]

    题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...

  9. POJ 1066 Treasure Hunt [想法题]

    题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...

随机推荐

  1. 安装Xamarin.Android几个经典介面

    昨晚Microsoft MVP的身份来申请Xamarin.Android,想不到今早就有邮件回复.花上些少时间订阅与注册: 望有时间能学习到一些新技术. 下面是Insus.NET下载并安装,留下几个经 ...

  2. [SinGuLaRiTy] 2017 百度之星程序设计大赛 复赛

    [SinGuLaRiTy-1038] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Arithmetic of Bomb Problem D ...

  3. 在一个java类里,private int a; 什么时候要使用integer

    private Integer index; if(index == null) index = 0; else this.index = index; Integer有一个明显的好处,就是它能比in ...

  4. 【转】C#日期时间格式化

    源地址:https://www.cnblogs.com/polk6/p/5465088.html

  5. scrapy 调试功能

    在使用 scrapy 来爬取网页的时候,我们难免会使用到调试功能,下面介绍两种调试方法: 1.终端使用 scrapy shell exampleurl exampleurl 为你要爬取网站的 url ...

  6. networkX如何读取存储图的二进制.dat文件

    一般情况下,.dat文件存储的是图的二进制邻接矩阵. import networkx as nx G = nx.readadjlist('auth_graph.dat')

  7. The server of Apache (三)——网页优化

    在企业中,部署apache后只采用默认的配置参数,会有很多问题,因为那些配置都是针对以前服务器配置的. 一.网页压缩 1.介绍 配置apache的网页压缩功能,是使用Gzip压缩算法来对apache服 ...

  8. vue 路由导航白话全解析

    这里先放上官网的教程和说明:点击这里,vue导航守卫官方文档 路由守卫 路由守卫说白了就是路由拦截,在地址栏跳转之前 之后 跳转的瞬间 干什么事 全局守卫 全局守卫顾名思义,就是全局的,整个项目所有路 ...

  9. LightOJ - 1197 素数筛

    深夜无事可干啊 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; typedef long lon ...

  10. 1144 The Missing Number (20 分)

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...