HackerRank - candies 【贪心】
HackerRank - candies 【贪心】
Description
Alice is a kindergarten teacher. She wants to give some candies to the children in her class. All the children sit in a line (their positions are fixed), and each of them has a rating score according to his or her performance in the class. Alice wants to give at least 1 candy to each child. If two children sit next to each other, then the one with the higher rating must get more candies. Alice wants to save money, so she needs to minimize the total number of candies given to the children.
Input Format
The first line of the input is an integer N, the number of children in Alice’s class. Each of the following N lines contains an integer that indicates the rating of each child.
Constraints
Output Format
Output a single line containing the minimum number of candies Alice must buy.
Sample Input 0
3
1
2
2
Sample Output 0
4
Explanation 0
Here 1, 2, 2 is the rating. Note that when two children have equal rating, they are allowed to have different number of candies. Hence optimal distribution will be 1, 2, 1.
Sample Input 1
10
2
4
2
6
1
7
8
9
2
1
Sample Output 1
19
Explanation 1
Optimal distribution will be 1,2,1,2,1,2,3,4,2,1
题意
有N个小朋友,老师要给小朋友发糖,然后每个小朋友都有一个对应分数,并且他们有一个序列。如果一个小朋友两边坐着的是分数比他低的,那么他的糖数就一定要多于那个人。求老师最少要发的糖数。
思路
先顺序扫一遍,求每个小朋友的左边,连续比他分数高的人有多少个。 EG:6 5 4 2 3 比如这组数据 分数为4的小朋友 他的左边有 6 5 这两个人的分数都比这个小朋友高 所以连续的人数就是2 而 分数为3的那个小朋友 左边是 6 5 4 2 虽然 左边一共有三个人分数比他高,但是离他最近的 并且连续的 分数比他高的 一个都没有。
所以这个小朋友 左边 连续分数比他高的人数就是0 。 然后再逆序扫一遍 求右边。
最后FOR一遍 求max(left, right) + 1 就可以了。 因为 最少是一颗糖
AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<numeric>
using namespace std;
const int MAX = 0x3f3f3f3f;
const int MIN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
typedef long long LL;
struct node
{
int num, left, right;
}q[maxn];
int main()
{
int n;
cin >> n;
int i;
scanf("%d", &q[0].num);
q[0].left = 0;
int temp = 0;
for (i = 1; i < n; i++)
{
scanf("%d", &q[i].num);
if (q[i].num > q[i- 1].num)
temp++;
else temp = 0;
q[i].left = temp;
}
temp = 0;
q[n - 1].right = 0;
for (i = n - 2; i >= 0; i--)
{
if (q[i].num > q[i + 1].num)
temp++;
else
temp = 0;
q[i].right = temp;
}
LL tot = n; //最后结果要用 long long 保存
for (i = 0; i < n; i++)
tot += max(q[i].left, q[i].right);
cout << tot << endl;
}
HackerRank - candies 【贪心】的更多相关文章
- Boxes and Candies(贪心)
Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...
- HackerRank# Candies
原题地址 LeetCode上也有这道题,直接扫一遍就行了,连数组都不用开,感觉像是蕴含了某种动归的思想在里面,要不怎么是个动归题呢 代码: #include <cmath> #includ ...
- atcoder题目合集(持续更新中)
Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...
- 2017 Bangladesh National High School Programming Contest ( National Round, Senior Group ), NHSPC 2017 题解
[题目链接] A. Charm Is Not Always Enough 模拟一下就可以了. #include <bits/stdc++.h> using namespace std; i ...
- HackerRank - greedy-florist 【贪心】
HackerRank - greedy-florist [贪心] 题意 有N个人 要去买K朵花.老板为了最大化新顾客的数量.就压榨回头客.每一朵花都有一个基本价格.一个顾客买下这朵花的价格是他来这里买 ...
- 2018.09.23 atcoder Boxes and Candies(贪心)
传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...
- *[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...
- Hackerrank 2020 February 2014 解题报告
Hackerrank 2020 February 2014 解题报告 比赛链接 Sherlock and Watson (20分) 题意:给定一个数组,向右平移K次,然后有Q个询问,问第x位置上是几 ...
- candy(贪心)
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- RabbitMQ OS X下安装及常用命令-1
RabbitMQ的主页在http://www.rabbitmq.com/ . 1. 安装Erlang RabbitMQ是用Erlang编写的,所以需要先安装Erlang,如果有的话跳过 ...
- jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
本文实例讲述了jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法.分享给大家供大家参考.具体实现方法如下: 这里主要通过val方法设置按钮的文字,并用attr方法修改disabled属性实 ...
- jquery书写一个简易的二级联动
先用php生成一个json数组示例如下 JSON_UNESCAPED_UNICODE 是对汉字进行处理的参数 然后HTML代码如下 把那个json_city赋值成我们用php生成的json即可 < ...
- Oracle中select使用别名
1 .将字段用as转换成别名. 2 .直接在字段的名字后面跟别名. 3 .在字段后面用双引号引起的别名. 我的朋友 大鬼不动 最近访客 fhwlj kochiyas 大極星 Alz__ deser ...
- Hadoop单点伪分布模式安装
Hadoop单点伪分布模式安装 概述 单点 single-node,单节点,即一台计算机. 伪分布式模式 pseudo-distributed mode 所谓集群,表面上看是多台计算机联合完成任务:但 ...
- dao---service---action分层结构
此文转载于http://blog.csdn.net/jay198746/article/details/4698709 之前有看过一些ssh2中采用dao---service---action分层结构 ...
- SAS9.4安装
安装教程请查看博客https://blog.csdn.net/qq_38960682/article/details/80567686 启动SAS时就报下面的错了:WARNING: 连接逻辑库“SAS ...
- Docker与virtualenv
docker环境统一,组件隔离 关于Docker与virtualenv,其本质是一样的,virtualenv虚拟python运行环境,保证系统python环境的整洁,docker也是一样,只是它虚拟的 ...
- 并发编程&数据库 - 考核题
第八章主要内容 第八章:线程.进程.队列.IO多路模型 操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列. 生产者消息者模型.异步模型. ...
- maven之jre默认配置
需要在用户或者全局settings.xml中做如下配置 例:用户配置: 添加代码: <profile> <id>jdk-1.8</id> <activatio ...