[codeforce 975C] Valhalla Siege (二分)
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 (二分)的更多相关文章
- codeforces 975C Valhalla Siege
题意: 有n个巫师站成一列,每个巫师有自己的血量. 一个人射箭攻击他们,每次造成若干点伤害,巫师按照给定的顺序承受伤害,如果伤害大了,那么死掉,伤害落到下一个巫师身上. 如果一轮攻击之后,所有的巫师都 ...
- Codeforces Round #478 C. Valhalla Siege
C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces-975C - Valhalla Siege 前缀和 思维
C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforce 702C Cellular Network 二分答案
http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...
- Codeforces Round #478 Div2 975A 975B 975C 975D
A. Aramic script 题目大意: 对于每个单词,定义一种集合,这个集合包含且仅包含单词中出现的字母.给你一堆单词,问有多少种这种集合. 题解: 状压,插入set,取size #in ...
- Codeforces Round #478 (Div. 2) ABCDE
A. Aramic script time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #478 (Div. 2)
题目链接:http://codeforces.com/contest/975 A. Aramic script time limit per test:1 second memory limit pe ...
- CF练习记录
2018/5/6 Codeforces Round #478 (Div. 2) C http://codeforces.com/contest/975/problem/C Valhalla Siege ...
- codeforce 359D 二分+ 动态规划(sparse table)
原题链接:http://codeforces.com/problemset/problem/359/D 思路:首先对符合题目的长度(r-l)从0到n-1进行二分查找,对每一个长度进行check,看是否 ...
随机推荐
- no projects are found to import
从svn上导出的项目在导入Eclipse中常常出现 no projects are found to import . 产生的原因是:项目文件里中没有".classpath"和&q ...
- hdoj 1518 Square 【dfs】
题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形. 策略:深搜. hdoj 1455的简化版 代码: #include <stdio.h> #include <stri ...
- 弗拉特利定律:Illumina怎样缔造基因革命
蕾妮·瓦林特(Renee Valint)的女儿谢尔碧(Shelby)在2000年出生时.看起来虚弱无力,就如同一仅仅耷拉着的布娃娃.谢尔碧学着走路和说话,但学得很慢.错过了儿童发展的重要阶段.到4岁时 ...
- [POJ 1934] Trip
[题目链接] http://poj.org/problem?id=1934 [算法] 先用dp求出LCS,然后搜索即可,注意加上一些剪枝 [代码] #include <algorithm> ...
- .net core发布到IIS后502.5错误
net core 在win7系统发布后,出现在502.5错误. 打开“开始”菜单,搜索“事件查看器”,然后选择“事件查看器”应用. 在“事件查看器”中,打开“Windows 日志”节点. 选择“应用程 ...
- idea的环境变量设置(Enviroment variables)
- 8、List接口的特点及其相关功能
/* * Collection * |--List * 元素有序(指的是存储顺序和取出顺序是否一致),可重复. * |--Set * 元素无序,唯一. */ /* * List的特有功能: * A:添 ...
- Hadoop MapReduce编程 API入门系列之wordcount版本4(八)
这篇博客,给大家,体会不一样的版本编程. 是将map.combiner.shuffle.reduce等分开放一个.java里.则需要实现Tool. 代码 package zhouls.bigdata. ...
- guice基本学习,guice的学习资料(十)
这个是我前面几篇的参考. guice的学习资料下载:http://pan.baidu.com/s/1bDEPem 路途遥远,但是人确在走.不忘初心,方得始终.
- NFS 开机自动挂载共享目录
开机自动挂载: 如果服务端或客户端的服务器重启之后需要手动挂载,我们可以加入到开机自动挂载 在服务端/客户端的/etc/fstab里添加 192.168.22.204:/opt/filestore ...