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 ...
随机推荐
- hdu 2141:Can you find it?(数据结构,二分查找)
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- python 发送email
pyton smtplib发送邮件 在邮件中设置并获取到 smtp域名 在脚本中执行命名,收件人可以是 多个,在列表中 import smtplib from email.mime.text impo ...
- gcc安装(centos)
gcc 4.8 安装 [root@DS-VM-Node239 ~]# curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repo ...
- 类库服务寄宿到WebHost
1.该Demo中包含一个类库项目.一个空的WebForm项目 2.新建WebForm项目 3.全局路由中注册类库服务 public class Global : System.Web.HttpAppl ...
- shell脚本学习总结05--变量与环境变量
bash中一切变量都是以字符串的形式存储,env命令可以查看与此终端进程相关的环境变量. man bash 查看一个进程的环境变量 1.获得程序的PID,例如Java# pgrep java23492 ...
- java基础---->Java中异常的使用(一)
今天我们大致学习一下java中关于异常的知识.原来忍住一段时间不联系一个人,真的就不想联系了. java异常的使用 一.java异常的一些说明 .Throwable 类是Java 语言中所有错误或异常 ...
- Android 获取当前应用的版本号和当前系统的版本号
1.获取当前程序版本名 我们可以在AndroidManifest.xml中设置程序的版本号等,如android:versionName="1.0",那如果想在代码中获取这个版本号呢 ...
- CH5201 数组组合【01背包】
5201 数字组合 0x50「动态规划」例题 描述 在N个数中找出其和为M的若干个数.先读入正整数N(1<N<100)和M(1<M<10000), 再读入N个正数(可以有相同的 ...
- 防止Form中嵌入WebBrowser出错导致程序崩溃
siow(1253366) 10:11:13两种方法你用的自带的webbrowser还是embeded那个毛小毛(3335076) 10:12:15或者有什么办法拦截到是webbrowser,如 ...
- Tortoise SVN 使用
1.添加文件或文件夹 2.删除文件或文件夹 ①If you want to delete an item from the repository, but keep it locally as an ...