您的位置:首页 > 理论基础 > 数据结构算法

【Codeforces Round 271 (Div 2)B】【前缀和二分 or 暴力】Worms 每种类型多个个数第i个数什么类型

2015-12-08 10:27 471 查看
B. 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.

Sample test(s)

input
5
2 7 3 4 9
3
1 25 11


output
1
5
3


Note
For the sample input:

The worms with labels from [1, 2]
are in the first pile.

The worms with labels from [3, 9]
are in the second pile.

The worms with labels from [10, 12]
are in the third pile.

The worms with labels from [13, 16]
are in the fourth pile.

The worms with labels from [17, 25]
are in the fifth pile.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=1e6+10,Z=1e9+7,ms63=1061109567;
int a
;
int n,m;
void BF()
{
int b[M];
int p=0;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
int x;scanf("%d",&x);
while(x--)b[++p]=i;
}
scanf("%d",&m);
for(int i=1;i<=m;++i)
{
int x;scanf("%d",&x);
printf("%d\n",b[x]);
}
}
int main()
{
BF();return 0;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;++i)scanf("%d",&a[i]),a[i]+=a[i-1];
scanf("%d",&m);
for(int i=1;i<=m;++i)
{
int x;scanf("%d",&x);
printf("%d\n",lower_bound(a+1,a+n+1,x)-a);
}
}
return 0;
}
/*
【trick&&吐槽】
有时候,暴力做法反而更优呢。
要因材施教!噗。

【题意】
给你n(1e5)个类型的物品,第i个类型的物品的数量在[1,1000]范围。
物品依次排列,物品数量之和不超过1e6。
现在有m(1e5)个询问,问你第i个物品是哪种类型。

【类型】
前缀和二分or暴力

【分析】
这题是可以前缀和二分的,就是我们求出前i个物品共计有多少个。
然后,当问我们第x个物品是什么类型的时候。
我们二分找到一个大于等于x的前缀和,对应的下标就是第i个物品的类型啦。

然而,事实上,这种做法并不好。
因为这个复杂度是O(mlogn)。
鉴于物品数量并不多,最好的做法是直接记下第i个数是什么类型。
看似更暴力了,实际复杂度却只有O(∑a[i]+m),对于大数据反而更优哦。

【时间复杂度&&优化】
O(mlogn) O(∑a[i]+m)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息