474B Worms

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

题意分析:输入n意为有n堆虫,之后的第二行各个数字为每堆的虫的数量,第三行为要搜索的虫子的个数,最后一行一次是各个要找的虫子的位序。我们要求的是每个要找的虫子分别在第几堆里面。

算法思路:这是一个一个简单的标签式存储和搜索的题目,利用两个数组,一个用来存所有虫子的位置,另外一个数组用来存对应虫子的组别,直接输出即可。

需要注意的是,输入和分组的操作要同步执行,不能在下面查找的过程中去搜索全部,不然会超时。

下面出示超时代码:

 1 #include<iostream>
2 using namespace std;
3 int n, a[100000], m, b[100000], sum[100000];//n代表堆的数目,a[]存储每堆中虫的数目,m表示上等虫的个数,b[]存储每个上等虫的位置
4 int main()
5 {
6 cin >> n;
7 for (int i = 0;i < n;i++)
8 {
9 cin >> a[i];
10 if (i == 0)sum[i] = a[i];
11 else sum[i] = a[i] + sum[i - 1];
12 }
13 cin >> m;
14 for (int i = 0;i < m;i++)
15 {
16 cin >> b[i];
17 }
18 for (int i = 0;i < m;i++)
19 {
20 for (int j = 0;j < n-1;j++)
21 {
22 if (b[i] <=sum[0])
23 {
24 cout << 1 << endl;
25 break;
26 }
27 else if(b[i] > sum[j] && b[i]<=sum[j + 1])
28 {
29 cout << j + 2<<endl;
30 break;
31 }
32 }
33 }
34 return 0;
35 }

显然可见: 在输入之后每一个虫子的搜索都从头开始的行为是一定会超时的。下面出示ac代码:

 1 #include<iostream>
2 using namespace std;
3
4 int n, m;
5 int a[100010], ans[1000010];
6
7 int main() {
8 cin >> n;
9 for (int i = 1; i <= n; i++) {
10 cin >> a[i];
11 a[i] += a[i - 1];
12
13 for (int j = a[i - 1] + 1; j <= a[i]; j++)
14 ans[j] = i;
15 }
16 cin >> m;
17 for (int i = 1; i <= m; i++) {
18 int tmp;
19
20 cin >> tmp;
21 cout << ans[tmp] << endl;
22 }
23 }

这题要注意的就是标签式存储的思路和注意超时问题,其他都不太难的

Worms的更多相关文章

  1. B. Worms Codeforces Round #271 (div2)

    B. Worms time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  2. Codeforces Round #271 (Div. 2)-B. Worms

    http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...

  3. Codeforces 474B. Worms

    It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mo ...

  4. Codeforces 271 Div 2 B. Worms

    题目链接:http://codeforces.com/contest/474/problem/B 解题报告:给你n个堆,第i个堆有ai个物品,物品的编号从1开始,第一堆的编号从1到a1,第二堆编号从a ...

  5. Can of Worms 【迭代/线段树】

    题意:一条直线上有n个炸弹,给出每个炸弹的爆炸半径,可以引爆另一个炸弹爆炸.问:每个炸弹爆炸后,最多有几个炸弹一起爆炸? 迭代,用线段树更新. #include <cstdio> #inc ...

  6. Codeforces 474B Worms 二分法(水

    主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...

  7. hdu 4404 Worms(多边形与圆的交)

    求出爆炸点的坐标,就成了多边形与圆相交面积的模板题了... #include<algorithm> #include<iostream> #include<cstring ...

  8. CodeForces 474B Worms (水题,二分)

    题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...

  9. HDU3109: Worms(字符串变换类 DP)

    pro:开始有一个字母虫,然后字母虫在每一天可以选择自己身上的部分字母变换,变换规则形如A->BC. 现状给定最终字母虫的字符串,求最少用了多少天. 如有规则A->BC,B->AC, ...

随机推荐

  1. 在quasar 注册全局filter

    A common use case for Quasar applications is to run code before the root Vue app instance is instant ...

  2. 详解nohup /dev/null 2>&1 含义的使用

    https://www.jb51.net/article/169837.htm 这篇文章主要介绍了详解nohup /dev/null 2>&1 含义的使用,文中通过示例代码介绍的非常详细 ...

  3. Shell 里空语句怎么写 - 半角的冒号

    Python 里的空语句写作pass for x in range(10): pass Shell 里的空语句写作 : #!/bin/bash for x in {1..10} do #echo $x ...

  4. 聊聊docker那些端口问题

    今天来系统聊一聊docker的端口,常见的有容器内程序端口.容器端口.主机端口.Dockerfile中EXPOSE端口.docker-compose和docker run中的port等. 貌似很多端口 ...

  5. LINUX学习--nginx服务器的安装

    一.安装环境 操作系统CentOS6.8 关闭SeLinux和iptables防火墙 二.网络yum源 将下面的软件下载到  /etc/yum.repos.d/   的目录下 官方基础:http:// ...

  6. 【Java】多态性

    文章目录 多态性 向下转型 多态性 可以理解为一个事物的多种形态. 对象的多态性:父类的引用指向子类的对象.只适用于方法,不适用于属性(编译和运行都看左边) 总结:对于对象的多态性,编译,看左边:运行 ...

  7. 《剑指offer》面试题65. 不用加减乘除做加法

    问题描述 写一个函数,求两个整数之和,要求在函数体内不得使用 "+"."-"."*"."/" 四则运算符号. 示例: 输 ...

  8. Rancher Fleet使用教程

    官方文档: https://fleet.rancher.io/ https://github.com/rancher/fleet 博客截止日期为:20201204 当前官网版本为v0.3.0,但在实践 ...

  9. gin框架中的渲染

    各种数据格式的响应 json.结构体.XML.YAML类似于java的properties.ProtoBuf 点击查看代码 // json响应 func someJson(context *gin.C ...

  10. golang中内存地址计算-根据内存地址获取下一个内存地址对应的值

    package main import ( "fmt" "unsafe" ) func main() { // 根据内存地址获取下一个字节内存地址对应的值 da ...