CF975C Valhalla Siege 题解

  • 题目
    • 链接
    • 字面描述
      • 题目描述
      • 输入格式
      • 输出格式
      • 样例 #1
        • 样例输入 #1
        • 样例输出 #1
      • 样例 #2
        • 样例输入 #2
        • 样例输出 #2
      • 提示
  • 思路
  • 代码实现(更简)
  • 代码实现

题目

链接

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;elser=mid-1;}if(l-1>=n){cout << n << endl ;sum=0;}elsecout << 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;
}

CF975C Valhalla Siege 题解相关推荐

  1. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  2. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  3. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  4. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  5. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  6. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  7. [洛谷1383]高级打字机 题解

    题解 这道题一看就珂以用主席树啊 这是一道神奇的题目,那么我们先敲一个主席树,然后维护一个数组len,表示下一次应该在len + 1插入, 之后对于T操作,在上一个版本的len + 1上直接执行插入 ...

  8. luogu P1549 棋盘问题(2) 题解

    luogu P1549 棋盘问题(2) 题解 题目描述 在\(N * N\)的棋盘上\((1≤N≤10)\),填入\(1,2,-,N^2\)共\(N^2\)个数,使得任意两个相邻的数之和为素数. 例如 ...

  9. 【题解搬运】PAT_L1-009 N个数求和

    从我原来的博客上搬运.原先blog作废. (伪)水题+1,旨在继续摸清这个blog(囧 题目 就是求N个数字的和.麻烦的是,这些数字是以有理数"分子/分母"的形式给出的,你输出的和 ...

最新文章

  1. node给java发送文件_如何实现node上传文件到后台?
  2. Flutter快速入门 五步搞定Flutter环境配置
  3. 利用JDBC连接数据库(MySQL)
  4. 构建并用 TensorFlow Serving 部署 Wide Deep 模型
  5. 360 屏蔽ajax,怎么在easy ui做全局Ajax拦截啊?
  6. fpu测试_I510400性能及温度测试详解
  7. CSDN-Markdown编辑器使用小技巧
  8. 操作系统——实验叁——主存空间的分配与回收
  9. Springboot+dubbo Zookeeper+Docker
  10. 从一条select语句看Oracle数据库查询工作原理
  11. 微信小程序阻止默认冒泡事件
  12. Android项目中加入弹幕功能
  13. 对Git暂存区的理解
  14. 人工智能没成果,年底的PPT怎么写?腾讯科学家张潼离职的后续讨论
  15. 新年寄语 —— 奋斗2020
  16. 傻瓜式解决pycrypto安装错误
  17. 修改 QQ 聊天记录保存路径
  18. 链表---给定一个排序链表,删除所有重复的元素每个元素只留下一个
  19. 产品:《人人都是产品经理》读书笔记
  20. 中国的LPR改革及其意义

热门文章

  1. MapReduce关系代数运算——投影
  2. coreldraw x8里线段显示尺寸_CorelDRAW X8实现轮廓线的粗细变化的方法
  3. 【杂篇 · 技巧】WebStorm页面窗口与显示bug
  4. 利用阿里公有云建设灾备中心的最佳实践
  5. PC VR游戏的CPU性能分析与优化
  6. uniapp微信小程序视频播放卡顿
  7. 理解单模光纤:基本概念篇
  8. Render函数的用法
  9. 为什么文件删除了但磁盘空间没有释放?
  10. 通过PS把月亮“搬”到自己的床上