原题地址

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的更多相关文章

  1. HackerRank - candies 【贪心】

    HackerRank - candies [贪心] Description Alice is a kindergarten teacher. She wants to give some candie ...

  2. 【POJ2886】Who Gets the Most Candies?-线段树+反素数

    Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...

  3. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  4. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  5. 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 ...

  6. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  7. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  8. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

  9. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

随机推荐

  1. viewpager的使用-新方法 5.1

    效果图: 添加依赖包: compile ‘com.android.support:design:22.2.0‘ 布局文件: <?xml version="1.0" encod ...

  2. 电脑连接海信电视 HDMI

    注意:我们家的电视是海信的,所以不能代表所有的电视哦~~~ 家里电视有线电视已经过期很长时间了,早就想把电脑连接到电视上用电视做显示器的心了,今天来兴趣了,就弄了一下!!! 用电脑连接电视需要先解决两 ...

  3. @AutoWired注解使用时可能会发生的错误

    @Autowired注解:用于对Bean的属性变量.属性的set方法及构造函数进行标注,配合对应的注解处理器完成Bean的自动配置工作.@Autowired注解默认按照Bean类型进行装配.  1.在 ...

  4. flask--Django 基本使用

    #导入flaskfrom flask import Flask #创建应用 app = Flask(__name__) #创建根路径视图 @app.route('/') def hello_world ...

  5. nuxt 头部引入js文件 第一次进入页面不加载js文件的解决方法

    head () { return { title: '', meta: [ { hid: 'description', name: 'description', content: '' } ], sc ...

  6. 树状数组 简单题 cf 961E

    题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...

  7. shell脚本,如何监控mysql数据库。

    [root@localhost wyb]# cat jkmysql #!/bin/bash status=`/etc/init.d/mysqld status|grep running|wc -l` ...

  8. 人脸识别中的检测(在Opencv中加入了QT)

    #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...

  9. 学习笔记之30个常用的maven命令

    maven 命令的格式为 mvn [plugin-name]:[goal-name],可以接受的参数如下, -D 指定参数,如 -Dmaven.test.skip=true 跳过单元测试: -P 指定 ...

  10. grub加密。

    一.介绍 安全无小事  linux系统的安全分为很多方面,什么端口啊,什么网络啊,听着都特么烦,今天谈谈最简单明显的密码安全. 二.单用户模式 单用户模式个人觉得相当有用,可以用来修复系统,修改密码… ...