code forces 474B Worms

此题链接:http://codeforces.com/problemset/problem/474/B


B. Worms

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.

Examples

Input

Copy

5
2 7 3 4 9
3
1 25 11

Output

Copy

1
5
3

我所理解的题目意思和注意点:

  • 有N堆美味的虫子(每堆编号为1~N),每堆虫子数量为a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 10^6),每个虫子都有编号从第一堆的1号 到第N堆的a1+a2+......+an号,现在Marmot知道有m只虫子很肥美,分别是第q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an)只虫子。请输出这些肥美的虫子是第几堆。
  • 要注意的是二分的判断情况。

#include<stdio.h>            // 第一次通过的binarysearch代码。(参考了挑战程序设计)
int main()
{
    int i,j,n,m,sum=0;
    scanf("%d",&n);
    int a[100100],b[100100];   // 5
    for(i=0;i<n;i++)           // 2 7 3 4 9
        scanf("%d",&a[i]);     // 2 9 12 16 25
    scanf("%d",&m);            // 1 25 11
    for(i=0;i<m;i++)
        scanf("%d",&b[i]);

    for(i=0;i<n;i++)
    {
        sum=sum+a[i];                  // 第i+1堆虫子之前有多少只。
        a[i]=sum;
    }
    for(i=0;i<m;i++)
    {
        int l=0,r=n;
        while(r-l>=1)
        {
            j=(l+r)/2;
            if(a[j]==b[i])
            {printf("%d\n",j+1);break;}
            else if(a[j]<b[i])
            {
                if(b[i]<=a[j+1])
                {
                    printf("%d\n",j+2);
                    break;
                }
                else l=j+1;
            }
            else
            {
                if(j==0)
                 {
                     printf("%d\n",j+1);break;
                 }
                 if(b[i]>a[j-1])
                 {
                     printf("%d\n",j+1);
                     break;
                 }
                else r=j;
            }
        }
    }
    return 0;
}

这是其他csdn大佬的代码:

#include<stdio.h>
#define maxn 100005          // csdn 大佬写的代码(膜拜)*>_<*。
int n;
int m;
int a[maxn];                 //  2 9 12 16 25
                             //  1 25 11
int binsearch(int tofind)
{
    int high=n,low=1;
    if(tofind<=a[1]) return 1;
    while(1)
    {
        int mid=(high+low+1)/2;
        if(high==low+1) return high;
        else if(a[mid]==tofind) return mid;
        else if(a[mid]>tofind)
        {
            high=mid;
        }
        else if(a[mid]<tofind)
        {
            low=mid;
        }
    }
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(i>1)
            a[i]+=a[i-1];
    }
    cin>>m;
    for(int j=0;j<m;j++)
    {
        int k;
        cin>>k;
        cout<<binsearch(k)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LaoXiangQ/article/details/81383495