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 ...
随机推荐
- Red Hat系统安装Redis
环境 RHLinux-6.4-64-EN, 红帽6.4 64位,英文正式公布版 安装 安装非常easy,先下载redis的压缩包,下载地址见这里.然后复制到你的linux机器.接着运行以下的命令. 1 ...
- js函数柯里化
function curry(fn){ // 代码 } function add(a,b,c){ return a + b + c; } const execAdd = curry(add); exe ...
- iOS UIWebView 获取内容实际高度,关闭滚动效果
本文转载至 http://my.oschina.net/Khiyuan/blog/341535 iOS UIWebView 获取内容实际高度,关闭滚动效果 近期做东西,将 UIWebView 嵌套 ...
- win上gulp配置
主线: 安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 1,安装node.js 1.1.说明 ...
- 【BZOJ2783】[JLOI2012]树 DFS+栈+队列
[BZOJ2783][JLOI2012]树 Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节 ...
- Arduino开发版学习计划--蓝牙控制小车行走
蓝牙模块一共6个引脚,我们一般只需要接4个线就可以了,分别是VCC.GND.TXD.RXD这四个引脚,我们分别接到arduino板子上,VCC接3.3V,GND接板子的GND,蓝牙TXD接板子的RXD ...
- 160525、高并发之mysql主从复制(linux)
第一步:新建两台linux主机(我这里是使用虚拟机,linux版本是CentOS-6.3-x86_64-bin-DVD1.iso,注意:我下载的是dvd1,如果其他版本默认安装可能会自动还原) 第二步 ...
- Android中的 style 和 theme
通过设置 view 控件的属性,达到设置android UI的目的,如果某些 属性值复用率很高,可以考虑将属性单独声明在 style中,这样就可以达到复用的效果. 一.style Style 概念:A ...
- 唯品会的Service Mesh三年进化史 2018 年 Service Mesh 元年,被誉为是下一代微服务架构
2018 年 Service Mesh 元年,被誉为是下一代微服务架构 https://www.sohu.com/a/225324586_465914 唯品会的Service Mesh三年进化史 - ...
- Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #in ...