-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worms.java
62 lines (59 loc) · 1.68 KB
/
Worms.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package worms.acm;
import java.util.Scanner;
/**
*
* @author shakil
*/
public class WormsACM {
/**
* @param args the command line arguments
*/
public static int binarySearch(int[] a,int low, int high, int key){
int middle=low+(high-low)/2;
if(low==high){
return low;
}
if(low==high-1){
if(a[middle]>=key){
return low;
}else{
return high;
}
}
if(a[middle]>=key){
return binarySearch(a, low, middle, key);
}else{
return binarySearch(a, middle, high, key);
}
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int[] pileOfWorms=new int[n];
for(int i=0;i<n;i++){
if(i==0){
int temp=input.nextInt();
pileOfWorms[i]=temp;
}else{
int temp=input.nextInt();
pileOfWorms[i]=pileOfWorms[i-1]+temp;
}
}
int m=input.nextInt();
int[] juicyWorms=new int[m];
for(int j=0;j<m;j++){
int lable=input.nextInt();
juicyWorms[j]=lable;
}
for(int k=0;k<m;k++){
int position=binarySearch(pileOfWorms, 0, n-1, juicyWorms[k]);
System.out.println(++position);
}
}
}