HackerRank# Candies
LeetCode上也有这道题,直接扫一遍就行了,连数组都不用开,感觉像是蕴含了某种动归的思想在里面,要不怎么是个动归题呢
代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
int curr_score, prev_score, curr_cnt;
int dir, len;
int res = ; prev_score = -;
curr_cnt = ;
dir = ;
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &curr_score);
if (curr_score > prev_score) {
if (dir < ) {
res += ( - curr_cnt) * (len + (int) (curr_cnt <= ));
curr_cnt = ;
}
curr_cnt += ;
res += curr_cnt;
dir = ;
} else if (curr_score < prev_score) {
if (dir >= )
len = ;
curr_cnt -= ;
res += curr_cnt;
dir = -;
len += ;
} else {
if (dir < )
res += ( - curr_cnt) * (len + (int) (curr_cnt <= ));
curr_cnt = ;
res += curr_cnt;
dir = ;
}
prev_score = curr_score;
}
if (dir < )
res += ( - curr_cnt) * (len + (int) (curr_cnt <= )); cout << res << endl; return ;
}
HackerRank# Candies的更多相关文章
- HackerRank - candies 【贪心】
HackerRank - candies [贪心] Description Alice is a kindergarten teacher. She wants to give some candie ...
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
随机推荐
- UIView动画效果之----翻转.旋转.偏移.翻页.缩放.取反的动画效
翻转的动画 //开始动画 [UIView beginAnimations:@"doflip" context:nil]; //设置时常 [UIView setAnimationDu ...
- sql中保留2位小数
问题: 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round() 函数,如 Round(@num,2 ...
- Hibernate学习之简单应用
前言:博主在学到Spring的时候,要开始做项目了,突然觉得好像有点虚,之前学过的Hibernate框架的简单应用好像又忘记了.所以返回来,做个小笔记. 简单来讲,Hibernate框架是利用对象-关 ...
- 《算法图解》中涉及的算法的总结及java实现
该项目源代码已经放到Github上,有兴趣可以点击AlgorithmGraphExample 进行访问 项目启动,项目使用maven搭建,如果不使用maven导入,请保证有Junit4的jar包在工程 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- C03 程序逻辑
程序逻辑 运算符 顺序结构 选择结构 循环结构 运算符 赋值运算符:= 比较运算符:>.<.==. >=.<=.!= 逻辑运算符:&&.||.! 顺序结构 在C ...
- Java形式参数和返回值的问题
形式参数和返回值的问题 (1).形式参数: A.类名:需要该类的对象. B.抽象类名:需要该类的子类对象. C.接口名:需要该接口的实现类对象. A.类名作为形式参数 class Student { ...
- C# 使用Epplus导出Excel [2]:导出动态列数据
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- Python爬虫环境常用库安装
1:urllib urllib.request这两个库是python自带的库,不需要重新安装,在python中输入如下代码: import urllibimport urllib.requestres ...
- Perl学习之四:语句(续)
循环控制:1.last 退出标签的语句块2.next 3.redo不推荐,循环次数不可控 4.goto不推荐.***************************************标签: 先 ...