原题地址

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. Selenium私房菜系列7 -- 玩转Selenium Server

    本篇主要是想更进一步介绍Selenium Server的工作原理,这次我们从Selenium Server的交互模式开始. 在<第一个Selenium RC测试案例>中,我们以命令“jav ...

  2. hihoCoder hiho一下 第四十六周 博弈游戏·Nim游戏·三( sg函数 )

    题意: 给出几堆石子数量,每次可以取走一堆中任意数量的石头,也可以将一堆分成两堆,而不取.最后取走者胜. 思路: 先规矩地计算出sg值,再对每个数量查SG值就可以了.最后求异或和.和不为0的就是必赢. ...

  3. 新建maven的pom.xml第一行出错的解决思路

    前言:博主在想要用maven创建项目的时候,忘记之前已经安装过maven了,所以再安装了另一个版本的maven,导致在pom.xml的第一行总是显示某一个jar的zip文件读取不出来. 在网上找了很多 ...

  4. DRBD+NFS+Keepalived高可用环境

    1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...

  5. ssh的server安装和安装指定版本的软件的方法

    ssh程序分为有客户端程序openssh-client和服务端程序openssh-server.如果需要ssh登陆到别的电脑,需要安装openssh-client,该程序ubuntu是默认安装的.而如 ...

  6. Codeforces Round #272 (Div. 2)-B. Dreamoon and WiFi

    http://codeforces.com/contest/476/problem/B B. Dreamoon and WiFi time limit per test 1 second memory ...

  7. javascript基本数据类型问题汇总

    isNaN()检测是否是NaN: 比较浮点相等,用绝对值,是否小于某一个阈值 Math.abs(1/3 - (1-2/3))<0.0000001: 字符串多行显示\n,ES6中使用反引号``: ...

  8. python已安装好第三方库,pycharm import时仍标红的解决办法

    pip install pymysql之后导入import pymysql时候标红 发现 pymysql下方还是标红,不能正常导入 可以试用一下以下的办法 解决办法: 首先打开 Settings找到P ...

  9. Windows10+anaconda,python3.5, 安装glove-python

    Windows10+anaconda,python3.5, 安装glove-python安装glove安装之前 Visual C++ 2015 Build Tools开始安装安装glove最近因为一个 ...

  10. HTML5拖放(drag和drog)

    拖放(drag和drog)是HTML5的标准的组成部分,也是种常见的特性,意义为抓起一个元素放入到另外的一个位置,在HTML5中任何元素都可以被拖放,前题是要相关进行设置. 1.设置元素为可拖放,也就 ...