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 ...
随机推荐
- maven 3.3.9版本下载地址
请使用迅雷下载 http://www-us.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
- 【数据库-Azure SQL Database】JDBC 如何连接 SQL Azure 数据库
使用 JAVA 代码连接 Azure SQL Database 时产生了 SSL 错误,对于此问题大多数用户都是因为不知如何编写 JDBC 连接字符串而产生的,以下为相关示例代码,供您参考: pa ...
- 补充---spring多线程任务调度
在spring任务调度的基础上增加多线程 三种方式: (1)使用OpenSymphony Quartz 调度器 (2)使用JDK Timer支持类 (3)SpringTaskExecutor抽象 sp ...
- ubuntu16.0 安装 openstack
主要参考官方文档:https://docs.openstack.org/liberty/zh_CN/install-guide-ubuntu/environment-nosql-database.ht ...
- 在Windows笔记本上调试运行在iOS设备上的前端应用
我在每天工作中需要在不同的移动设备上测试我们开发的前端应用是否正常工作,比如iOS设备和Android设备.我用的工作笔记本电脑又是Lenovo的,安装的是Windows操作系统. 有的时候一个开发好 ...
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
- Objective-C相关Category的收集(更新)
Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是Fille ?str?m,是@ IMGNRY的联合创 ...
- Javascript 日期格式化
Javascript 日期格式化 需求: 给出:日期 .格式,根据日期格式进行输出. Date.prototype.Format = function (fmt) { //author: meizz ...
- vue2.0的变化
1. 在每个组件模板,不在支持片段代码 组件中模板: 之前: <template> <h3>我是组件</h3><strong>我是加粗标签</st ...
- caffe修改需要的东西
https://blog.csdn.net/zhaishengfu/article/details/51971768?locationNum=3&fps=1