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. hisql 新一代无实体ORM使用第一步 hisql安装使用

    安装 github hisql 最新源码下载 也可以通过nuget安装 注意:HiSql仅支持.net5或以上环境 选择您需要支持的数据库对应的支持包进行安装 本例使用sqlserver进行演示,请安 ...

  2. 通过了解Servlet和Http之间的关系,了解web中http通信使用

    注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6512336761551585796/ 1.<Servlet简单实现开发部署过程> 2.<Serv ...

  3. day7 对字母数字的编排

    1.函数fun()的功能:在s数组内寻找并且统计t数组在s数组中出现的次数 输入字符串:函数scanf()函数不好用,会出现很多bug,因此不使用scanf() 效果理想: 2.函数fun()功能:将 ...

  4. 一文搞清楚 DNS 的来龙去脉

    目录 美国霸权 ICANN:互联网界的联合国 IP 地址分配 域名解析架构 分层架构: DNS 缓存: 根 DNS 服务器: 顶级 DNS 服务器(TLD): 权威 DNS 服务器: 本地 DNS: ...

  5. Sentry 开发者贡献指南 - 数据库迁移

    Django 迁移是我们处理 Sentry 中数据库更改的方式. Django 迁移官方文档:https://docs.djangoproject.com/en/2.2/topics/migratio ...

  6. mate10碎屏机当成小电脑使用尝试

    1.屏碎了修起来300-400,自己动手至少也要260以上买个屏幕钱. 手机图案锁屏也不知道密码,给我手机的亲戚忘了.当年手机被车压弯了. 对着恢复教程,盲屏幕猜着按还原了. 2.之后一路从8代系统更 ...

  7. Linux 标准输入输出、重定向

    一 相关知识 1)默认地,标准的输入为键盘,但是也可以来自文件或管道(pipe |). 2)默认地,标准的输出为终端(terminal),但是也可以重定向到文件,管道或后引号(backquotes ` ...

  8. maven常用打包命令

    常用maven命令 执行与构建过程(编译,测试,打包)相关的命令必须进入pom.xml所在位置执行 mvn clean:清理(打包好的程序放在生成的名为target的文件中,清理即删除文件中打包好的程 ...

  9. 学习JAVAWEB 第三十六天

    今天改了一天的bug 使用eclipse出现的问题:首先lib文件夹的名字是不可以更改的它放在WEB-INF文件夹下,放所有的jar包,使用时一定不要忘了将jar包添加至构建路径tomcat的部署问题 ...

  10. [luogu P1312]Mayan游戏

    其实就是一道锻炼码力的简单题-- 看到题目中的\(0<x\leqslant 5\)也就知道是爆搜了吧( 我们仿照写游戏的方法多写几个函数,能够有效降低错误率(确信 我们写出大致的搜索流程来: 如 ...