CF975C Valhalla Siege 题解
题目
链接
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;
}