Codeforces Round #446 (Div. 2) B. Wrath【模拟/贪心】
2 seconds
256 megabytes
standard input
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.
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.
Print one integer — the total number of alive people after the bell rings.
4
0 1 0 10
1
2
0 0
2
10
1 1 3 0 0 0 2 1 0 3
3
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【模拟/贪心】的更多相关文章
- Codeforces Round #446 (Div. 2)
Codeforces Round #446 (Div. 2) 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如redbag和ShichengXiao)交流的--希望下次能自己做出来2333 ...
- 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 ...
- 【Codeforces Round #446 (Div. 2) B】Wrath
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...
- 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 ...
- 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 ...
- Codeforces Round #543 (Div. 2) D 双指针 + 模拟
https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...
- Codeforces Round #398 (Div. 2) A. Snacktower 模拟
A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...
- Codeforces Round #237 (Div. 2) B题模拟题
链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...
- Codeforces Round #371 (Div. 2) C 大模拟
http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...
随机推荐
- 关于 Google Chrome “Your connection is not private” 问题的处理
今天下午访问google网站的时候,突然不能访问了,提示“Your connection is not private”(你的连接不是私密连接):查看XX-NET的设置,显示“请检查浏览器代理设置”. ...
- unity2D技术学习与整理
目前有关unity2D的教程以及原理几乎都是国外的.我在这方面也是新手,看了一些例子有很多不懂的地方. 这个网站提供的教程很有参考价值 http://brackeys.com/ 还有这个 http:/ ...
- 【Luogu P2257】YY 的 GCD
题目 求: \[ \sum_{i = 1}^n \sum_{j = 1}^m [\gcd(i, j) \in \mathbb P] \] 有 \(T\) 组数据, \(T\le 10^4, n, m\ ...
- swift中的正则表达式
swift中的t正则表达式 正则表达式是对字符串操作的一种逻辑公式,用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规则字符串" ...
- windows下Tomcat安装
环境Windows 64位 jdk1.8 1.Tomcat安装 官网地址:http://tomcat.apache.org/download-90.cgi 下载安装包,安装之后进行解压 2.修改htt ...
- 常用模块(sys)
import sys# sys.argv() # 命令参数List,第一个元素是程序本身路径,如:python test.py run db# sys.exit('shh') # 退出程序,正常退出时 ...
- python3编写脚本之--------购物车
我这里的购物车的大概内容是: 1.首先要知道商品的有什么东西可卖,需要多少钱. 2.买家准备花多少钱去购物商品,购买的商品先放在购物车里. 3.最后买家还可以是否确定购买购物车的东西,还剩多 ...
- java5初学
1.Eclipse快捷键 alt + / 代码提示,例如自动创建main方法ctrl + d 删除当前行ctrl + alt + up/down 复制当前行alt + up/down 交换行ctrl ...
- php 根据文件内容来判断文件类型
/*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.doc.ppt *6 ...
- jquery serialize() 方法
ajax异步提交的时候,会使用该方法. 方法:jQuery ajax - serialize() 方法