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 } 

  • 相关阅读:
    c# PrintDocument 设置自定义纸张大小的示例
    C#获取本地打印机列表,并将指定打印机设置为默认打印机
    水晶报表自定义纸张大小打印 (Crystal Report Print with custom paper size)
    c#打印机设置,取得打印机列表及相应打印机的所有纸张格式
    在C#中设置打印机纸张大小
    打印grid
    获取List集合中最大值的方法
    mysql使用索引优化查询效率
    mysql数据库中标的key的含义
    mysql数据库添加索引优化查询效率
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4046903.html
Copyright © 2011-2022 走看看