Codeforces Round #271 (Div. 2)-B. Worms
http://codeforces.com/problemset/problem/474/B
1 second
256 megabytes
standard input
standard output
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile.
The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot.
The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms.
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
5
2 7 3 4 9
3
1 25 11
1
5
3
For the sample input:
- The worms with labels from [1, 2] are in the first pile.
- The worms with labels from [3, 9] are in the second pile.
- The worms with labels from [10, 12] are in the third pile.
- The worms with labels from [13, 16] are in the fourth pile.
- The worms with labels from [17, 25] are in the fifth pile.
解题思路:a1, a2, a3...an个worms分别在第1, 2, 3....n个pile上,问你q1, q2, q3...qm条worms在第几个pile上,分别算出到第1, 第2, 第3。。。 第n个pile总共有几条worms,然后二分即可
1 #include <stdio.h>
2
3 int p[];
4
5 int bin_search(int num, int l, int r){
6 int mid;
7 while(l <= r){
8 mid = (l + r) >> ;
9 if(num > p[mid-] && num <= p[mid]){
break;
}
if(p[mid] > num){
r = mid -;
}
else if(p[mid] < num){
l = mid + ;
}
}
return mid;
}
int main(){
int n, a, m, q, i;
while(scanf("%d", &n) != EOF){
scanf("%d", &p[]);
for(i = ; i < n; i++){
scanf("%d", &a);
p[i + ] = p[i] + a;
}
scanf("%d", &m);
while(m--){
scanf("%d", &q);
printf("%d\n", bin_search(q, , n));
}
}
return ;
37 }
Codeforces Round #271 (Div. 2)-B. Worms的更多相关文章
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #271 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/474 A题:Keyboard 模拟水题. 代码例如以下: #include <iostream> #include ...
- Codeforces Round #271 (Div. 2)
A. Keyboard 题意:一个人打字,可能会左偏一位,可能会右偏一位,给出一串字符,求它本来的串 和紫书的破损的键盘一样 #include<iostream> #include< ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #271 (Div. 2) D. Flowers (递推)
题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #271 (Div. 2)-A. Keyboard
http://codeforces.com/problemset/problem/474/A A. Keyboard time limit per test 2 seconds memory limi ...
- Codeforces Round #271 (Div. 2) F ,E, D, C, B, A
前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...
随机推荐
- 学习RadonDB源码(二)
1. 为我新的一天没有放弃而喝彩 学习是一件很容易放弃的事情,因为就算是不学,我也能在现在的岗位上发光发热.可是人不就是一个热爱折腾的种群吗? 今天没有放弃不代表明天没有放弃,也许放弃的可能性大于坚持 ...
- 解读大内老A的《.NET Core框架本质》
老A说的一句话让我很受启发,想要深入了解框架,你要把精力聚焦在架构设计的层面来思考问题.而透彻了解底层原理,最好的笨办法就是根据原理对框架核心进行重建或者说再造.看起来没有捷径,也是最快的捷径. 题外 ...
- (水题)洛谷 - P2439 - 阶梯教室设备利用 - 简单dp
https://www.luogu.org/fe/problem/P2439 很明显时间是一个维度,按照时间顺序决策就行了. dp[i]表示以时间i为结尾所能达到的最长演讲时间. #include & ...
- MongoDb Samus 驱动的改进
一直使用 MongoDb 的 Samus C#驱动. 其有一个缺陷,就是无法支持struct的读写. 但是一般数据都用Class包装,所以也没有太在意. 随着这些天尝试写入 KLineData 时,遇 ...
- [Xcode 实际操作]九、实用进阶-(2)遍历设备(输出系统)上的所有字体
目录:[Swift]Xcode实际操作 在实际工作中,经常需要调整界面元素的字体种类. 本文将演示输出系统提供的所有字体,方便检索和使用. 在项目导航区,打开视图控制器的代码文件[ViewContro ...
- DOM0、DOM2级事件
JavaScript DOM0.DOM2级事件 1.DOM0级事件:on+事件类型 在html行内直接绑定,也就是通过行内js绑定的例如<span onclick="alert('1' ...
- web前端篇:JavaScript基础篇(易懂小白上手快)-1
目录 详细内容: 0.JavaScript的引入 1.第一个JavaScript 2.变量 3.变量的类型 4.数组 5.条件语句 6.三元运算符 7.循环 8.函数 9.对象(object): 10 ...
- 即时通讯新手入门:一文读懂什么是Nginx?它能否实现IM的负载均衡?
本文引用了“蔷薇Nina”的“Nginx 相关介绍(Nginx是什么?能干嘛?)”一文部分内容,感谢作者的无私分享. 1.引言 Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡 ...
- C 语言实例 - 复数相加
C 语言实例 - 复数相加 C 语言实例 C 语言实例 使用结构体(struct)将两个复数相加. 我们把形如 a+bi(a,b均为实数)的数称为复数,其中 a 称为实部,b 称为虚部,i 称为虚数单 ...
- morphia(3)-查询
1.查询所有 @Test public void query() throws Exception { final Query<Employee> query = datastore.cr ...