BJFU 1549 ——Candy——————【想法题】
Candy
总提交: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
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——————【想法题】的更多相关文章
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- HDU - 5969 最大的位或 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- CodeForces - 156B Suspects 逻辑 线性 想法 题
题意:有1~N,n(1e5)个嫌疑人,有m个人说真话,每个人的陈述都形如X是凶手,或X不是凶手.现在给出n,m及n个陈述(以+x/-X表示)要求输出每个人说的话是true ,false or notd ...
- 2016华中农业大学预赛 E 想法题
Problem E: Balance Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 205 Solved: 64[Submit][Status][We ...
- codeforces 657C - Bear and Contribution [想法题]
题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...
- POJ 1066 Treasure Hunt [想法题]
题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...
随机推荐
- C#ADO.NET基础一
简介 使用SQLite进行讲解 1.基础类: SQLiteConnection 连接数据库 SQLiteCommand 执行命令(增,删,改,查),或存储过程 SQLiteDataReader 读取查 ...
- React进阶篇(1) -- react-router4模块化
本篇内容: 单一的路由无嵌套 多层嵌套路由 获取路径中的参数 按需加载 单一的路由无嵌套 routers.js import Home from 'components/Home'; import N ...
- 51 nod 1267 4个数和为0
1267 4个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出& ...
- Nginx 进行性能配置
总所周知,网络上我们购买的服务器的性能各不相同,如果采用 Nginx 的默认配置的话,无法将服务器的全部性能优势发挥出来,我们应该选择适合自己需求的配置. 当我们默认安装后 Nginx 后,我们便得到 ...
- MySQL学习笔记(一):查询
查询实例: 1.创建数据库并使用: create database school; use school; 2.创建表并插入内容: create table student( Sno char(9) ...
- 【Thread】线程工厂-ThreadFactory
ThreadFactory---线程工厂 在apollo源码中有这么一段代码 ExecutorService m_longPollingService = Executors.newSingleThr ...
- 用shell脚本安装apache
我们首先创建一个文件为test.sh,执行此文件的方法有以下四种方式: 1../test.sh(必须chmod赋予执行权限) 2.. test.sh 3.sourse test.sh 4.[shell ...
- loj #2538. 「PKUWC2018」Slay the Spire
$ \color{#0066ff}{ 题目描述 }$ 九条可怜在玩一个很好玩的策略游戏:Slay the Spire,一开始九条可怜的卡组里有 \(2n\) 张牌,每张牌上都写着一个数字\(w_i\) ...
- Linux 常用命令大全(长期更新)
常见指令 打包压缩相关命令 关机/重启机器 Linux管道 vim使用 用户及用户组管理 文件权限管理 更改文件的用户及用户组 更改权限 常用指令 ls 显示文件或目录 -l 列出文件详细信息l(li ...
- [SDOi2012]Longge的问题 BZOJ2705 数学
题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). ...