Codeforces Round #162 (Div. 2)
发布日期:2021-06-30 15:14:36 浏览次数:2 分类:技术文章

本文共 942 字,大约阅读时间需要 3 分钟。

题目很简单,求吃掉所有坚果最少时间,只要按照题意进行模拟就行。注意理解一句话:

  • Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1.

这句话的意思是说,跳向下一棵树,在跳的过程中,Liss所处的高度不变。更正规的说就是,当Liss在第i棵树的h高度的时候,她只能跳向第i+1棵树的高为h的地方。注意当h>hi+1的时候不可行。不要理解成当后面的树比前边的树矮就不能跳!无论前边比后边矮还是高都能跳!前边的树高那就先往下走,走到和后边一样高的时候再跳即可。最少时间当然就是走树顶吃坚果的时间最少。

之所以总结一下,就是因为在做类似模拟的时候,需要考虑能否简化一下代码。

#include
using namespace std;#define max 100010typedef long long ll;int h[max];int main(){ int n, i; ll time=0; cin >> n; for (i = 0; i
> h[i]; time += h[0] + 1; //上树 for (i = 1; i
注意到代码中for循环的部分是可以简化的,都是求差加2操作,故可以用绝对值来简化代码如下:

#include
using namespace std;#define max 100010typedef long long ll;int h[max];int main(){ int n,i; ll time = 0; cin >> n; for (i = 0; i
> h[i]; time += h[0] + 1; if (n>1) { for (i = 1; i

转载地址:https://jianzhuwang.blog.csdn.net/article/details/45127225 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Codeforces Round #163 (Div. 2)
下一篇:next_permutation 的使用

发表评论

最新留言

很好
[***.229.124.182]2024年04月17日 07时25分33秒