B. Wrath
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Hands that shed innocent blood!

There are n guilty people in a line, the i-th of them holds a claw with length Li. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j < i and j ≥ i - Li.

You are given lengths of the claws. You need to find the total number of alive people after the bell rings.

Input

The first line contains one integer n (1 ≤ n ≤ 106) — the number of guilty people.

Second line contains n space-separated integers L1, L2, ..., Ln (0 ≤ Li ≤ 109), where Li is the length of the i-th person's claw.

Output

Print one integer — the total number of alive people after the bell rings.

Examples
input
4
0 1 0 10
output
1
input
2
0 0
output
2
input
10
1 1 3 0 0 0 2 1 0 3
output
3
Note

In first sample the last person kills everyone in front of him.

【题意】: n个人排队,第i个人会杀死第j个人当且仅当j < i并且j ≥i-li,问最后有多少个人能幸存下来。

【分析】:从后往前扫,这样就避免了j<i的干扰,记录一下i-Li的最小值,就能判断第j个人是否会被杀死。

//

The i'th person will be alive if min(j - Lj) > i over all j > i.

Consider you know the jth person is alive or not if j > i and you have x = min(j - Lj) over all j > i. If x > i then the ith person will be alive.

And you can update x easily.

【代码】:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int inf = 0x7fffffff;
const int N = 1e6+;
int a[N],b[N],maxn;
int main()
{
int n,ans;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]); int maxn = n-a[n];
ans = ;
for(int i=n-;i>=;i--)//倒序,求活,即死的反面min(j - Lj) > i over all j > i.
{
if(i<maxn)//你的刀还无法触及我
{
ans++;//存活√
}
maxn = min(maxn,i-a[i]);//想要活?维护一个最小刀长,诶嘿嘿砍不到我~
}
printf("%d\n",ans);
return ;
}

Codeforces Round #446 (Div. 2) B. Wrath【模拟/贪心】的更多相关文章

  1. Codeforces Round #446 (Div. 2)

    Codeforces Round #446 (Div. 2) 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如redbag和ShichengXiao)交流的--希望下次能自己做出来2333 ...

  2. Codeforces Round #446 (Div. 2) A. Greed【模拟】

    A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  3. 【Codeforces Round #446 (Div. 2) B】Wrath

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...

  4. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  5. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  6. Codeforces Round #543 (Div. 2) D 双指针 + 模拟

    https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...

  7. Codeforces Round #398 (Div. 2) A. Snacktower 模拟

    A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...

  8. Codeforces Round #237 (Div. 2) B题模拟题

    链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...

  9. Codeforces Round #371 (Div. 2) C 大模拟

    http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...

随机推荐

  1. PJSIP-PJMEDIA【使用pjmedia 播放wav格式的音乐】

    应宝哥建议以及更好的交流学习,这篇开始使用中文,英语就先放一放吧! 要使用PJSIP中的PJMEDIA首先我们需要搭建好它所需要的环境. [环境搭建与调试] 1 在 工具 加入pjmedia所需要的包 ...

  2. 架构师速成7.3-devops为什么很重要 分类: 架构师速成 2015-07-07 17:22 410人阅读 评论(0) 收藏

    evops是一个很高大上的名字,其实说的简单点就是开发和运维本身就是一个团队的,要干就一起把事情干好.谁出了问题,网站都不行.作为一个架构师,必须要devops,而且要知道如何推行devops. 首先 ...

  3. 遍历列表,打印:我叫name,今年age岁,家住dizhi,电话phone(我是通过下标取键得到对应值,有哪位大神来个更简单的)

    lt = [ {'name':'小王', 'age':18, 'info':[('phone', '123'), ('dizhi', '广州')]}, {'name':'小芳', 'age':19, ...

  4. Python——数据类型之list、tuple

    本篇主要内容 •  list初识 •  list元素的访问 •  list内部所有的方法 •  tuple介绍和与list用法的比较 我觉得Python里面用的最多的就是List了,感觉好强大.他能存 ...

  5. vue 实例 网站

    Pure vue demo 实战第一节:Vue基础一 Pure vue demo 实战第二节:Vue基础二 Pure vue demo 实战第三节:Vue组件 Pure vue demo 实战第四节: ...

  6. token的作用

    token的作用 基于 Token 的身份验证方法 使用基于 Token 的身份验证方法,在服务端不需要存储用户的登录记录.大概的流程是这样的: 客户端使用用户名跟密码请求登录 服务端收到请求,去验证 ...

  7. 浅析_tmain() 与 main() 函数的区别

    _tmain()是为了支持Unicode所使用的main的一个别名,既然是别名,应该有宏定义过的,在<stdafx.h>里 #include <stdio.h> #indlud ...

  8. [luogu4242] 树上的毒瘤

    题目描述 这棵树上有n个节点,由n−1条树枝相连.初始时树上都挂了一个毒瘤,颜色为ci.接下来Salamander将会进行q个操作. Salamander有时会修改树上某个点到另外一个点的简单路径上所 ...

  9. 【CZY选讲·棋盘迷宫】

    题目描述 一个N*M的棋盘,’.’表示可以通过,’#’表示不能通过,给出Q个询问,给定起点和终点,判断两点是否联通,如联通输出“Yes”,否则输出“No”. 数据范围 N,M <=500,Q ...

  10. Postfix+Sasl+Courier-authlib+Dovecot+MySQL+extmail 邮件系统部署

    # yum remove postfix ##删除系统自带postfix# userdel postfix# groupdel postdrop# groupadd -g 2525 postfix# ...