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. 基于CentOS7.x gitlab环境搭建,卸载,汉化 --汉化篇

    gitlab环境搭建,卸载,汉化--汉化篇 注意gitlab的版本需和汉化版本一致 安装git yum install -y git 下载最新的汉化包 cd git clone https://git ...

  2. PowerShell【Do While、Do Until篇】

    1 $num=0 2 while($num -le 10) 3 { 4 $num 5 $num+=1 6 } 1 $num=0 2 do 3 { 4 $num 5 $num+=1 6 } 7 whil ...

  3. js几种常见排序的实现

    1. 冒泡排序 定义: 比较相邻的前后二个数据,如果前面数据大于后面的数据,就将二个 数据交换. 这样对数组的第0个数据到N-1个数据进行一次遍历后,最大的一个数据就"沉"到数组第 ...

  4. SYCOJ570传纸条

    题目-传纸条 (shiyancang.cn) 算法(线性DP) O(n3)O(n3)首先考虑路径有交集该如何处理.可以发现交集中的格子一定在每条路径的相同步数处.因此可以让两个人同时从起点出发,每次同 ...

  5. Ubuntu 18.04 server安装+搭建Seacms v10.1网站

    0x00 写在前面 以前我天真的认为,ubuntu Desktop会安装了,server就无所谓了,其实完全不然,server还是有一些坑点的. 之所以选择Seacms搭建网站,是因为这个SeaCMS ...

  6. Kafka connector (kafka核心API)

    前言 Kafka Connect是一个用于将数据流输入和输出Kafka的框架.Confluent平台附带了几个内置connector,可以使用这些connector进行关系数据库或HDFS等常用系统到 ...

  7. PWA 技术落地!让你的站点(Web)秒变APP(应用程序)

    Web应用方兴未艾,我们已经十分习惯习惯了在电脑上进行以自己的工作,而随着众多功能强大的在线网站,我们的Windows的桌面也不再拥挤着各种快捷方式:不光是PC端,在移动端我们也不再在浩如烟海的应用市 ...

  8. DbHelper 标准类

    import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.jsp.jstl.sql.*; public ...

  9. golang gin框架中实现"Transfer-Encoding: chunked"方式的分块发送数据到浏览器端

    参考了这篇帖子: https://golangtc.com/t/570b403eb09ecc66b90002d9 golang web如何发送小包的chunked数据 以下是代码: r.GET(&qu ...

  10. gin中的多模板和模板继承的用法

    1. 简单用法 package main import ( "github.com/gin-contrib/multitemplate" "github.com/gin- ...