Examples

input

5 5

1 2 1 2 1

3 10 1 1 1

output

3

5

4

4

3

input

4 4

1 2 3 4

9 1 10 6

output

1

4

4

1

Note

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.

题目大意:

有一列n个勇士,承受q分钟的箭击,第i个人承受值为a[i],第i分钟会有攻击值为k[i]的箭击,当第i个人受到a[i]的箭击后将死亡(倒下),该分钟剩余的攻击或下一分钟的攻击将会作用于死亡人后面的人身上,当某分钟所有人均死亡时,将会在该分钟满血复活,该分钟剩余的攻击值无效,即该分钟后所有人满血站着(由第二个样例可知复活可触发无限次)

现在给出n,q,a[i],k[i]输出每分钟结束后还站着的人数

先对a数组求前缀和

对每个k二分,寻找到第一个能承受住的人(或恰好go die的人)

记录last表示二分到的人受到攻击后剩余血量

sum表示从最近一次全体满血到现在为止箭击的总攻击量

注意各种情况即可

code:(cf比赛赶时间打的比较丑,懒得改了)

//Menteur_Hxy
#include <cstdio>
#include <iostream>
#include <algorithm>
#define ll long long
#define f(i,a,b) for(register int i=a;i<=b;i++)
using namespace std; inline ll rd() {
ll x=0,fla=1; char c=' ';
while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
return x*fla;
} inline void out(ll x){
int a[25],wei=0;
if(x<0) putchar('-'),x=-x;
for(;x;x/=10) a[++wei]=x%10;
if(wei==0){ puts("0"); return;}
for(int j=wei;j>=1;--j) putchar('0'+a[j]);
putchar('\n');
} const int MAX=2000100;
int n,q;
ll a[MAX],k[MAX]; int main() {
n=rd(),q=rd();
f(i,1,n) a[i]=rd()+a[i-1];
ll sum=0,l=1,r=n,last=0;
f(i,1,q) {
ll k=rd();
if(k >= a[n]-sum) {
out(n);
sum=0,l=1,r=n,last=0;
continue;
}
while(l<r) {
int mid=(l+r)>>1;
if(a[mid]-sum >= k) r=mid;
else l=mid+1;
}
sum+=k;r=n;
last=a[l]-sum;
// cout<<l<<" "<<r<<" "<<sum<<" "<<last<<" "<<las<<" ";
out(last==0?n-l:n-l+1);
if(!last) l++;
}
return 0;
}

[codeforce 975C] Valhalla Siege (二分)的更多相关文章

  1. codeforces 975C Valhalla Siege

    题意: 有n个巫师站成一列,每个巫师有自己的血量. 一个人射箭攻击他们,每次造成若干点伤害,巫师按照给定的顺序承受伤害,如果伤害大了,那么死掉,伤害落到下一个巫师身上. 如果一轮攻击之后,所有的巫师都 ...

  2. Codeforces Round #478 C. Valhalla Siege

    C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. Codeforces-975C - Valhalla Siege 前缀和 思维

    C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. codeforce 702C Cellular Network 二分答案

    http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...

  5. Codeforces Round #478 Div2 975A 975B 975C 975D

    A. Aramic script 题目大意:   对于每个单词,定义一种集合,这个集合包含且仅包含单词中出现的字母.给你一堆单词,问有多少种这种集合. 题解:   状压,插入set,取size #in ...

  6. Codeforces Round #478 (Div. 2) ABCDE

    A. Aramic script time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #478 (Div. 2)

    题目链接:http://codeforces.com/contest/975 A. Aramic script time limit per test:1 second memory limit pe ...

  8. CF练习记录

    2018/5/6 Codeforces Round #478 (Div. 2) C http://codeforces.com/contest/975/problem/C Valhalla Siege ...

  9. codeforce 359D 二分+ 动态规划(sparse table)

    原题链接:http://codeforces.com/problemset/problem/359/D 思路:首先对符合题目的长度(r-l)从0到n-1进行二分查找,对每一个长度进行check,看是否 ...

随机推荐

  1. [Drupal]主题教程

    drupal6和drupal7的主题开发有很大不同,本指南包含了这些不同 drupal7的默认主题是Bartik,6的是Garland drupal的主题系统是如何工作的 这部分内容主要讲述的是dru ...

  2. win7配置免安装mysql5.6.19过程具体解释

    本文主要介绍免安装配置mysql5.6.19的过程,整个过程中自己出现非常多次失误,经过整理,现将一次可成功的过程记录下来,供大家參考. 准备 在mysq官网 http://dev.mysql.com ...

  3. 国产芯片任重道远 国科微SSD主控芯片的“追赶之路”(不能只提供一颗芯片,而是要将芯片、国密算法、固件Firmware、BIOS和操作系统紧密联系在一起,变成完整解决方案交给行业用户,才能真正体现自身的价值)

    集微网消息,“中国芯”战略之路道阻且长,踏入这个赛道的攻坚者们需要十年如一日的技术突破,需要集合产业势能,共同协作,方能建立中国核心技术真正的竞争力. 国产化之路任重道远,SSD芯片初见成效 信息时代 ...

  4. POJ3614 Sunscreen 贪心入门

    题目大意 给出一些区间和一些点,一个点如果在一个区间内,那么此两者可以匹配.问匹配数最大是多少. 题解 这样的题我们一般都是站在区间上去找与其配对的点.我们可以得到如下性质: 对于一段区间\([l_1 ...

  5. HDU1561 The more, The Better

    HDU1561 The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. 第6章 Spring MVC的数据转换、格式化和数据校验

    使用ConversionService转换数据 <%@ page language="java" contentType="text/html; charset=U ...

  7. C++ 指针 引用 变量引用

    变量引用: 引用的作用就是给变量起个别名,假如有一个变量a,想给它起个别名b,         可以这么写:int a;//定义a是整型变量.int &b=a;//声明b是a的引用. 上面就是 ...

  8. [Javascript] HTML5 地理位置定位(HTML5 Geolocation)原理及应用

    地理位置(Geolocation)是 HTML5 的重要特性之一,提供了确定用户位置的功能,借助这个特性能够开发基于位置信息的应用.今天这篇文章向大家介绍一下 HTML5 地理位置定位的基本原理及各个 ...

  9. js作业

    1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米var sum=0;i=0;a=0.0001;for(i=0;i<100;i++){ a=a*2; sum=sum ...

  10. Python环境设置-zlib not available

    今天在Linux中安Python3的时候,出现了一个错误:zipimport.ZipImportError: can‘t decompress data; zlib not available 网上找 ...