zoukankan      html  css  js  c++  java
  • Codeforces Round #271 (Div. 2)-B. Worms

    http://codeforces.com/problemset/problem/474/B

    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 ≤ 103a1 + 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 [12] are in the first pile.
    • The worms with labels from [39] are in the second pile.
    • The worms with labels from [1012] are in the third pile.
    • The worms with labels from [1316] are in the fourth pile.
    • The worms with labels from [1725] are in the fifth pile.

     解题思路:a1, a2, a3...an个worms分别在第1, 2, 3....n个pile上,问你q1, q2, q3...qm条worms在第几个pile上,分别算出到第1, 第2, 第3。。。 第n个pile总共有几条worms,然后二分即可

     1 #include <stdio.h>
     2 
     3 int p[100100];
     4 
     5 int bin_search(int num, int l, int r){
     6     int mid;
     7     while(l <= r){
     8         mid = (l + r) >> 1;
     9         if(num > p[mid-1] && num <= p[mid]){
    10             break;
    11         }
    12         if(p[mid] > num){
    13             r = mid -1;
    14         }
    15         else if(p[mid] < num){
    16             l = mid + 1;
    17         }
    18     }
    19     return mid;
    20 }
    21 
    22 int main(){
    23     int n, a, m, q, i;
    24     while(scanf("%d", &n) != EOF){
    25         scanf("%d", &p[1]);
    26         for(i = 1; i < n; i++){
    27             scanf("%d", &a);
    28             p[i + 1] = p[i] + a;
    29         }
    30         scanf("%d", &m);
    31         while(m--){
    32             scanf("%d", &q);
    33             printf("%d ", bin_search(q, 1, n));
    34         }
    35     }
    36     return 0;

    37 } 

  • 相关阅读:
    解析excel表格为DataSet
    easyui 上传文件代码
    上传文件后台代码
    easyui dialog
    C++ 强制设置文件大小
    std::function与std::bind
    glog 编译报错 ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
    Qt删除目录
    C++11 中的std::function和std::bind
    TortoiseGit 使用教程
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4046903.html
Copyright © 2011-2022 走看看