Codeforces Round #446 (Div. 2) B. Wrath(线段树,RMQ,区间最值)
来源:互联网 发布:qq阅读网络异常怎么办 编辑:程序博客网 时间:2024/05/16 06:27
Description
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
40 1 0 10
output
1
input
20 0
output
2
input
101 1 3 0 0 0 2 1 0 3
output
3
Note
In first sample the last person kills everyone in front of him.
思路
有一群人,给他们分别编号,当钟声响起,他们会用手里的刀杀人,给出了刀的长度,第i个人能杀第j个人的条件是:
j < i 且 j>=i-Li
所以我们利用RMQ或者线段树枚举从1到n之间的i-Li
的最小值,然后就可以判断这个人死不死,最后算出来死亡的总人数,再用总数减去就是答案
代码1(RMQ)
#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define ll long longusing namespace std;const int N=1e6+20;int a[N];int minsum[N][20];void RMQ(int num){ for(int j = 1; j < 20; ++j) for(int i = 1; i <= num; ++i) if(i + (1 << j) - 1 <= num) { minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]); }}int main(){ int n,sum=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); minsum[i][0]=i-a[i]; } RMQ(n); for(int i=1; i<n; i++) { int k=(int)(log(n-(i+1)+1.0)/log(2.0)); int minnum=min(minsum[i+1][k],minsum[n-(1<<k)+1][k]); if(i>=minnum)sum++; } printf("%d\n",n-sum); return 0;}
代码2(线段树)
#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define ll long longusing namespace std;const int N=1e6+20;int a[N],b[N];#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int MIN[N<<2];void pushup(int rt){ MIN[rt]=min(MIN[rt<<1],MIN[rt<<1|1]);}void build(int l,int r,int rt){ if(l==r) { MIN[rt]=b[l]; return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt);}int querymin(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) return MIN[rt]; int m=(l+r)>>1; int ans=inf; if(L<=m) ans=min(ans,querymin(L,R,lson)); if(R>m) ans=min(ans,querymin(L,R,rson)); return ans;}int main(){ int n,sum=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); b[i]=i-a[i]; } build(1,n,1); for(int i=1; i<n; i++) { int minnum=querymin(i+1,n,1,n,1); if(i>=minnum)sum++; } printf("%d\n",n-sum); return 0;}
- Codeforces Round #446 (Div. 2) B. Wrath(线段树,RMQ,区间最值)
- Codeforces Round #446 (Div. 2) B Wrath
- Codeforces Round #446 (Div. 2) B.Wrath
- Codeforces Round #446 (Div. 2)B. Wrath
- Codeforces-446(Div.2)-B-Wrath--(线段树区间更新)
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range (线段树区间最值)
- Codeforces Round #446 (Div. 2) B. Wrath (贪心)
- Codeforces Round #218 (Div. 2) (线段树区间处理)
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 线段树+RMQ区间最值问题
- RMQ区间最值(线段树)
- Codeforces Round #343 (Div. 2) B. Far Relative’s Problem (线段树+区间更新+单点查询)
- RMQ Codeforces Round #322 (Div. 2) B. Luxurious Houses
- Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1 线段树
- Codeforces Round #275 Div.1 B Interesting Array --线段树
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树求区间gcd)
- Codeforces Round #365 (Div. 2) D 线段树+离线 (区间内的数有哪些
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase【可持久化线段树,区间取反
- 爬虫LOL王者信息----Jupyter Notebook
- 使用Construct 2制作HTML5游戏——完善
- 理解 Java 序列化
- 机器学习: Logistic Regression--python
- Hadoop/MapReduce 及 Spark KMeans聚类算法实现
- Codeforces Round #446 (Div. 2) B. Wrath(线段树,RMQ,区间最值)
- 多个div 缓冲运动—变宽
- 干货:FLAG的风头过后,哪一家才是最好的公司?
- iOS开发-测量APP启动耗时
- Google Play Service中安全检测API的使用说明
- 感恩节到来,还在找一个和家人聚聚朋友叙叙的地方吗?
- daterangepick时间控件双日历设置时分秒
- 数据结构线性表顺序存储C++实现
- SDUT-3378&&SDUT2040