G - Worms CodeForces - 474B

G - Worms

Problem
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 a i worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a 1, worms in second pile are labeled with numbers a 1 + 1 to a 1 + a 2 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 a 1, a 2, …, a n (1 ≤ a i ≤ 103, a 1 + a 2 + … + a n ≤ 106), where a i 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 q 1, q 2, …, q m (1 ≤ q i ≤ a 1 + a 2 + … + a n), 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 q i is.

Examples
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.

Time limit 1000 ms
Memory limit 262144 kB

思路:用暴力法一定会超时的,所以这里采用了二分查找法

import java.util.Scanner;

public class G {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr1 = new int[n];
		long[] sum = new long[n];
		for (int i = 0; i < n; i++) {
			arr1[i] = sc.nextInt();
			if(i>0) sum[i] = arr1[i] + sum[i-1];
			else sum[i] = arr1[i];
		}
		int m = sc.nextInt();
		int[] arr2 = new int[m];
		for (int i = 0; i < arr2.length; i++) {
			arr2[i] = sc.nextInt();
			System.out.println(seek(arr2[i] , sum));
		}
		sc.close();
	}

	private static int seek(int x, long[] sum) {
		if(x == sum[sum.length-1]) {
			return sum.length;
		}
		if(x <= sum[0]) return 1;
		int len = sum.length;
		int middle = len/2;//中部指针
		int left = 0;//左指针
		int right = len-1;//右指针
		while(true) {
			if(x == sum[middle]) return middle+1;//中部指针的值恰好是需要找的值时
			else if(x < sum[middle]) {
				right = middle;
				middle = (middle + left)/2;
			}else {
				left = middle;
				middle =( middle + right ) / 2;
			}
			if(middle == left || middle == right) return right+1;//中部指针等于左指针或者右指针时,说明左右指针已经相邻
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值