趣味题:杀人范围 codeforces B. Wrath

题意

每个人都有一个长度为 li 的武器,相邻的两个人之间距离为 1 ,同一时间所有人使用武器攻击左边的人,问最后存活下来的人数。

显然,最右侧的人一定是可以存活下来的。

我们维护一个 cnt 代表右侧延伸到当前位置的武器长度,

  • 若 cnt>0 说明当前位置在别人的攻击范围内,否则 ans+1 。
  • 更新 cnt 为 max(cnt1,ai) 看对于 i 来说是否可以攻击到更远的位置。

时间复杂度 O(n) 。

#include
#include
#include
#define maxn 1000005
#define LL long long
#define mod 1000000007
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	int a[maxn];
	while(cin>>n)
	{
		for(int i=0;i>a[i];
		}
		int cnt=a[n-1];
		int res=1; 
		for(int i=n-2;i>=0;i--)
		{
			if(cnt==0)
				res++;
			cnt=max(cnt-1,a[i]);
		}
		cout << res << endl;
	}	
	return 0;	
} 




你可能感兴趣的:(acm)