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 ...
随机推荐
- char[]与TCHAR[]互相转换引发的一个问题!
软件的一个驱动由于开发的年代比较久一些,使用的是非Unicode编码,而当前新的软件使用的是Unicode编码,于是将非Unicode驱动用于Unicode软件上时,就出现了问题! 问题就出现在非 ...
- 通过HttpWebRequest在后台对WebService进行调用
目录: 1 后台调用Webservice的业务需求 2 WebService支持的交互协议 3 如何配置WebService支持的协议 4 后台对WebService的调用 4.1 SOAP 1.1 ...
- 获取本地的json并展示
我们知道在java中,有两种方式可以传输数据 1.json javaScript Object Notation 是以健值段的方式展示并显示数据的 2.xml 是以节点的方式展示并显示数据的 xml是 ...
- 用SQL语句创建触发器
--假设XSCJ数据库中增加一新表XS_HIS,表结构和表XS相同,用来存放从XS--表 --中删除的记录.创建一个触发器,当XS表被删除一行,把删除的记录写到日--志表XS_HIS中. CREATE ...
- dubbo项目实战代码展示
最近公司项目使用dubbo服务,于是就去网上搜索关于dubbo的相关资料,真的很多,但是对于很多人并不是很了解框架或者 不是太适合新手的片段代码,于是我就根据项目的相关内容把dubbo部分单独切出来, ...
- android开发环境之ADT安装,卸载,更新
http://hj198703.iteye.com/blog/1316991 1.官网:http://developer.android.com/sdk/index.html2.ADT组件在线安装 ...
- [SDOI2016 Round1] 征途[斜率优化]
2225. [SDOI2016 Round1] 征途 ★★★☆ 输入文件:menci_journey.in 输出文件:menci_journey.out 简单对比时间限制:1 s 内存 ...
- svn移动目录时如何保留原来的日志
[问题描述] 想将SVN下的文件夹A移动目录D下,同时保留文件夹A及其下面文件的SVN日志 [原来的做法] 将文件夹A直接拷贝到目录D,然后提交到SVN [原来做法的问题] 日志无 ...
- angularJs-脏检查
来自:http://www.cnblogs.com/liuyanan/p/4935652.html scope是一个指向应用model的object,也是表达式的执行上下文. scope被放置在一个类 ...
- POJ3272 Cow Traffic
题目链接:http://poj.org/problem?id=3272 题目意思:n个点m条边的有向图,从所有入度为0的点出发到达n,问所有可能路径中,经过的某条路的最大次数是多少.边全是由标号小的到 ...