[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,看是否 ...
随机推荐
- @Zookeeper可视化工具。 ZK 安装 node-zk-browser。2015.10.22亲测可用
zookeeper基本是基于API和console进行znode的操作,并没有一个比较方便的操作界面,这里也发现了taobao 伯岩写的一个工具,可以比较方便的查询zookeeper信息. 工具的开发 ...
- RubyMine中自动完成只输入部分字母
RubyMine中自动完成只输入部分字母 1,有下划线情况(其实看第二点跟下划线就关系不大了) 对于attr_reader之类的输入,输入attr之后,下划线可以不输入,然后输入r或者e都可以出来, ...
- VS2013 EF6连接MySql
1.安装mysql server下载地址 http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-winx64.zip 2.安装MySql的VS插件 ...
- 2016.04.13,英语,《Vocabulary Builder》Unit 13
cord, from the Latin word for 'heart'. concord, ['kɑːŋkɔːrd] n. 和睦, 公约 con-,'with'. discord, ['dɪskɔ ...
- [BZOJ 2100] Apple Delivery
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2100 [算法] Answer = min{ dist(PB,PA1) + dist( ...
- [POJ 2282] The Counting Problem
[题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...
- CTF-Mayday
打开下载的Mayday.txt文件: 温柔 知足突然好想你 拥抱突然好想你 拥抱温柔 知足温柔 知足突然好想你 拥抱突然好想你 拥抱温柔 知足温柔 知足突然好想你 拥抱突然好想你 拥抱温柔 ...
- OpenCASCADE 包说明
转载地址:http://www.cppblog.com/eryar/archive/2012/06/30/180916.html 一.简介 Introduction to Package gp gp是 ...
- C - Twins(贪心)
Problem description Imagine that you have a twin brother or sister. Having another person that looks ...
- MVC中Excel导入
1.在项目中添加对NPOI的引用,NPOI下载地址:http://npoi.codeplex.com/releases/view/38113. 前端代码 <div class="fil ...