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. 利用代码生成工具Database2Sharp生成ABP VNext框架项目代码

    我们在做某件事情的时候,一般需要详细了解它的特点,以及内在的逻辑关系,一旦我们详细了解了整个事物后,就可以通过一些辅助手段来提高我们的做事情的效率了.本篇随笔介绍ABP VNext框架各分层项目的规则 ...

  2. USB Tethering always displays grey when USB tethering type is Linux(EEM)

    USB Tethering always displays grey when USB tethering type is Linux(EEM) 1.Problem DESCRIPTION USB T ...

  3. 日K蜡烛图

    股票价格涨跌趋势,常用蜡烛图技术中的K线图来表示,分为按日的日K线.按周的周K线.按月的月K线等.以日K线为例,每天股票价格从开盘到收盘走完一天,对应一根蜡烛小图,要表示四个价格:开盘价格Open(早 ...

  4. 匿名函数托管 func-spring-boot-starter

    匿名函数托管 func-spring-boot-starter 项目地址 func-spring-boot-starter开源项目地址: https://gitee.com/yiur/func-spr ...

  5. python环境搭建以及jupyter notebook的安装和启动

    一.Python 环境搭建 本章节我们将向大家介绍如何在本地搭建Python开发环境. Python可应用于多平台包括 Linux 和 Mac OS X. 你可以通过终端窗口输入 "pyth ...

  6. ClassCastException: java.util.Date cannot be cast to java.sql.Date

    解决办法 /** * 单个方法,作用,根据输入的day:yyyy-mm-dd格式的字符日期,将数据库中的该天所有数据更新为0 * 0表示假期 * @param day * @throws SQLExc ...

  7. liunx查看哪个ip连接最多

    [root@centos6 /]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|so ...

  8. 人口信息普查系统-JavaWeb-四

    今天给大家分享前端人口登记页面,人口查询页面 人口登记 <%@ page language="java" contentType="text/html; chars ...

  9. Zookeeper 源码解析-环境准备

    为了深入学习Zookeeper,准备把zookeeper源码导入eclipse中学习 一.Ant环境准备 因为我自己用的是Mac,直接输入命令:brew install ant即可 如果是window ...

  10. LeetCode673

    LeetCode每日一题2021.9.20 LeetCode673. 最长递增子序列的个数 思路 在最长上升子序列的转移时,维护一个 cnt 数组,表示 以 i 结尾的最长上升子序列个数 f[i] 表 ...