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]的数字 ③给一 ...
随机推荐
- 玩转Node.js(三)
玩转Node.js(三) 上一节对于Nodejs的HTTP服务进行了较为详细的解析,而且也学会了将代码进行模块化,模块化以后每个功能都在单独的文件中,有利于代码的维护.接下来,我们要想想如何处理不同的 ...
- django orderby
https://www.douban.com/group/topic/44329052/
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- Nginx主要模块常用指令说明
核心模块(Core Modules): 主模块(Main Module):配置和服务器全局有关的一些参数,比如错误日志.进程.权限等 user worker_processes error_logsy ...
- PHP面向对象练习2
思路:构造函数完成数据库连接,增删改一个方法,查询一条记录一个方法,查询多条一个方法,sql执行失败则返回提示,并交出sql语句方便查错 代码: <?class dbcontroll{ priv ...
- 【bzoj1511】[POI2006]OKR-Periods of Words KMP-next数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6827027.html 题目描述 一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前 ...
- BZOJ 2243 染色(树链剖分好题)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 7971 Solved: 2990 [Submit][Stat ...
- [洛谷P2634][国家集训队]聪聪可可
题目大意:给你一棵树,随机选两个点,求它们之间路径长度是$3$的倍数的概率 题解:点分治,求出当前状态的重心,然后求出经过重心的答案,接着分治每棵子树.注意考虑重复计算的情况 卡点:无 C++ Cod ...
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- 【BZOJ 2458 最小三角形】
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1551 Solved: 549[Submit][Status][Discuss] Descripti ...