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 } 

  • 相关阅读:
    8月7号的练习:HDU 1069&&POJ 1636&&HDU 1031&&HDU 1051&&HDU 1551
    8月8号的线段树:HDU 1754&&POJ 3264&&HDU1166
    8月6号的题目:HDU 1003&& POJ 1050&&HDU 1800&&HDU 2036&& POJ 1088(记忆化搜索)
    HDU 1052
    背包问题九讲:
    一个人的旅行 HDU 2066 &&HDU Today HDU 2112
    8月3号的LCS,LIS,LICS:Longest Ordered Subsequence&&Common Subsequence&&Greatest Common Increasing Subsequence
    那些操蛋的搜索题目:逃离迷宫&&哈密顿绕行世界问题
    C语言栈调用机制初探
    linux0.11改进之四 基于内核栈的进程切换
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4046903.html
Copyright © 2011-2022 走看看