[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,看是否 ...
随机推荐
- net_->ForwardBackward()的大致梳理
net_->ForwardBackward()方法在net.hpp文件中 Dtype ForwardBackward() { Dtype loss; Forward(&loss); Ba ...
- #leetcode#Anagrames
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Android之UtilsRequesServicetHelp工具类
package com.example.getnetutil; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; ...
- WPF中ListBox ListView数据翻页浏览笔记(强调:是数据翻页,非翻页动画)
ListBox和ListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现. 一.重点 对于ListBox和ListView来 ...
- spy++ 句柄消息详解
使用spy++捕获到的消息详解 主要是今天正好自己用到. 原来也有用过SPY++查看消息,然后自己SendMessage或者PostMessage 直接发送消息给目标程序.但是原来一用就有效果,今天要 ...
- ORACLE 11g 生产中高水位线(HWM)处理
数据库中表不断的insert,delete,update,导致表和索引出现碎片.这会导致HWM之前有很多的空闲空间,而oracle在做全表扫描的时候会读取HWM一下的所有块,这样会产生更多的IO,影响 ...
- 乐字节-Java8核心特性实战-接口默认方法
JAVA8已经发布很久,是自java5(2004年发布)之后Oracle发布的最重要的一个版本.其中包括语言.编译器.库.工具和JVM等诸多方面的新特性,对于国内外互联网公司来说,Java8是以后技术 ...
- Unity3d gameObject
using UnityEngine; using System.Collections; public class test : MonoBehaviour { //print只能在MonoBehav ...
- seo在前端网页制作的应用
学习了慕客网上的“SEO在网页制作中的应用‘’,下面来进行小小的学习总结,顺便梳理下知识.所谓学而不思则罔思而不学则殆.下面开始正文. 一.搜索引擎的工作原理 搜索引擎的基本工作原理包括如下三个过程: ...
- CSS简单入门
- Java攻城狮学习路线 - 一. 什么是CSS CSS指层叠样式表(Cascading Style Sheets),定义如何显示HTML元素 二. CSS语法 /* 选择器 { 声明: 声明:}* ...