Worms
474B Worms
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.


题意分析:输入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的更多相关文章
- B. Worms Codeforces Round #271 (div2)
B. Worms time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 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 ...
- Codeforces 474B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mo ...
- Codeforces 271 Div 2 B. Worms
题目链接:http://codeforces.com/contest/474/problem/B 解题报告:给你n个堆,第i个堆有ai个物品,物品的编号从1开始,第一堆的编号从1到a1,第二堆编号从a ...
- Can of Worms 【迭代/线段树】
题意:一条直线上有n个炸弹,给出每个炸弹的爆炸半径,可以引爆另一个炸弹爆炸.问:每个炸弹爆炸后,最多有几个炸弹一起爆炸? 迭代,用线段树更新. #include <cstdio> #inc ...
- Codeforces 474B Worms 二分法(水
主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...
- hdu 4404 Worms(多边形与圆的交)
求出爆炸点的坐标,就成了多边形与圆相交面积的模板题了... #include<algorithm> #include<iostream> #include<cstring ...
- CodeForces 474B Worms (水题,二分)
题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...
- HDU3109: Worms(字符串变换类 DP)
pro:开始有一个字母虫,然后字母虫在每一天可以选择自己身上的部分字母变换,变换规则形如A->BC. 现状给定最终字母虫的字符串,求最少用了多少天. 如有规则A->BC,B->AC, ...
随机推荐
- .NET 云原生架构师训练营(模板方法 && 建造者)--学习笔记
目录 模板方法 源码 建造者 模板方法 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤 源码 https://github.com ...
- java同时替换多个字符串
参考资料: https://blog.csdn.net/qq_39390545/article/details/106020221 来自为知笔记(Wiz)
- linux中vim编辑器的翻页命令
Linux jdk查看文件的最后一行 输入$回车 查看文件的第一行 输入0或者1回车 向前翻页 Ctrl + f f为forw ...
- linux如何查看服务器当前的并发访问量
linux如何查看服务器当前的并发访问量 [root@localhost ~]# netstat -pnt | grep :80 | wc -l 2 [root@localhost ~]# netst ...
- mac 操作系统使用iterm(2)自动登录远程服务器
找一个目录创建一个普通的文件,例如 vi myprofile ,编辑以下内容 #!/usr/bin/expect set PORT 22 set HOST www.****.com(或者ip地址) s ...
- Go语言系列之知识框架
一.Go基础入门知识 二.变量和基本数据类型 三.流程控制语句 四.数组和切片 五.map的声明和使用 六.函数func方法 七.指针和地址 八.结构体 九.接口interface 十.并发神器gor ...
- Go语言系列之自定义实现日志库
日志库logo gitee地址传送门:https://gitee.com/zhangyafeii/logo 日志库需求分析 1. 支持往不同的地方输出日志 2. 日志分级别 Debug Trace I ...
- 痞子衡嵌入式:我入选了2021年度与非网(eefocus)星选创作者Top10
本周二「与非网」一个美女运营小姐姐加痞子衡微信,告知痞子衡评上了一个奖,让痞子衡把收件地址告诉她,她把证书寄过来. 昨天痞子衡收到了快递,拆开一看,原来是被评上了 与非网 2021 年度创作者,这个证 ...
- 使用.NET 6开发TodoList应用(30)——实现Docker打包和部署
系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 .NET 6 Web API应用使用最多的场景是作为后端微服务应用,在实际的项目中,我们一般都是通过将应用程序打包成docke ...
- [STM32F4xx 学习] SPI与nRF24L01+的应用
前面已经总结过STM32Fxx的特点和传输过程,下面以nRF24L01+ 2.4GHz无线收发器为例,来说明如何使用SPI. 一.nRF24L01+ 2.4GHz无线收发器的介绍 1. 主要特性 全球 ...