CF975C Valhalla Siege 题解

文章介绍了编程竞赛题目CF975CValhallaSiege,涉及到战士在战斗中承受箭矢攻击的模拟问题。战士们按顺序排列,每分钟会受到一定数量的箭矢攻击,每个战士有特定的承受箭矢能力。题目要求在每分钟后计算出存活的战士数量。文章提供了两种不同的代码实现来解决这个问题,一种是简单的二分查找优化,另一种则使用了前缀和优化方法来跟踪战士的剩余血量。
摘要由CSDN通过智能技术生成

题目

链接

https://www.luogu.com.cn/problem/CF975C

字面描述

题目描述

Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar’s warriors are falling in battle.

Ivar has $ n $ warriors, he places them on a straight line in front of the main gate, in a way that the $ i $ -th warrior stands right after $ (i-1) $ -th warrior. The first warrior leads the attack.

Each attacker can take up to $ a_i $ arrows before he falls to the ground, where $ a_i $ is the $ i $ -th warrior’s strength.

Lagertha orders her warriors to shoot $ k_i $ arrows during the $ i $ -th minute, the arrows one by one hit the first still standing warrior. After all Ivar’s warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar’s warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute $ t $ , they will all be standing to fight at the end of minute $ t $ .

The battle will last for $ q $ minutes, after each minute you should tell Ivar what is the number of his standing warriors.

输入格式

The first line contains two integers $ n $ and $ q $ ( $ 1 \le n, q \leq 200,000 $ ) — the number of warriors and the number of minutes in the battle.

The second line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_i \leq 10^9 $ ) that represent the warriors’ strengths.

The third line contains $ q $ integers $ k_1, k_2, \ldots, k_q $ ( $ 1 \leq k_i \leq 10^{14} $ ), the $ i $ -th of them represents Lagertha’s order at the $ i $ -th minute: $ k_i $ arrows will attack the warriors.

输出格式

Output $ q $ lines, the $ i $ -th of them is the number of standing warriors after the $ i $ -th minute.

样例 #1

样例输入 #1
5 5
1 2 1 2 1
3 10 1 1 1
样例输出 #1
3
5
4
4
3

样例 #2

样例输入 #2
4 4
1 2 3 4
9 1 10 6
样例输出 #2
1
4
4
1

提示

In the first example:

  • after the 1-st minute, the 1-st and 2-nd warriors die.
  • after the 2-nd minute all warriors die (and all arrows left over are wasted), then they will be revived thus answer is 5 — all warriors are alive.
  • after the 3-rd minute, the 1-st warrior dies.
  • after the 4-th minute, the 2-nd warrior takes a hit and his strength decreases by 1.
  • after the 5-th minute, the 2-nd warrior dies.

思路

有关量 : st 进展到满血士兵的角标,u st-1名士兵剩余血量
进行二分否则会超时

代码实现(更简)

#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll n,q,sum;
ll a[200010],k[200010];

int main(){
    cin >> n >> q ;
    for(int i=1;i<=n;i++){
        ll t ;
        cin >> t ;
        a[i]=a[i-1]+t;
    }
    for(int i=1;i<=q;i++){
        ll t ;
        cin >> t ;
        sum+=t;
        ll l=1,r=n;
        while(l<=r){
            ll mid=l+((r-l)>>1);
            if(a[mid]<=sum)
                l=mid+1;
            else
                r=mid-1;
        }
        if(l-1>=n){
            cout << n << endl ;
            sum=0;
        }
        else
            cout << n-l+1 << endl ;
    }
    return 0 ;
}

代码实现

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn=2e5+10;
int n,q,st;
ll u;//st-1名士兵剩余血量
ll a[maxn],k[maxn],f[maxn];
int main(){
	scanf("%d%d",&n,&q);
	//求士兵能承受箭的数量的前缀和
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		f[i]=f[i-1]+a[i];
	}
	st=1;//从第一个人开始承受
	for(int i=1;i<=q;i++){
		scanf("%lld",&k[i]);
		if(u>k[i])u-=k[i];
		else if(u==k[i]){
			u=0;
			++st;
		}
		else {
			if(k[i]>=f[n]-f[st-1]+u){//全死还不够
				st=1;
				u=0;
			} 
			else{
				int l=st ,r=n;
				while(l<=r){//二分
					int mid=l+r>>1;
					if(f[mid]+u-f[st-1]>k[i])r=mid-1;
					else if(f[mid]+u-f[st-1]==k[i])break;
					else l=mid+1;
				}
				//更新
				int m=l+r>>1;
				ll w=f[m]+u-f[st-1]-k[i];
				u=w;
				st=m+1;
			}
		}
		//全死统计
		if(st==n+1){
			st=1;
			u=0;
		}
		printf("%d\n",n-st+1);//输出剩余人数
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值