http://codeforces.com/problemset/problem/474/B
1 #include<stdio.h> 2 int num_bunch[100100]; //每堆虫子条数 3 int bound[100100]; //堆与堆之间的界限 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]
5 int find(int target, int bound[], int pile) 6 { 7 int low = 0, high = pile, middle; 8 while(low <= high) 9 { 10 middle = low + (high - low) / 2; 11 if(bound[middle] < target) low = middle + 1; 12 else if(bound[middle] > target) high = middle - 1; 13 else return middle; 14 } 15 return low; 16 } 17 18 int main() 19 { 20 int pile, i, juicy, target; 21 scanf("%d", &pile); 22 int cnt = 0; 23 num_bunch[cnt ++] = 0; 24 for(int i=0; i<pile; i++) 25 { 26 scanf("%d", &num_bunch[i]); 27 bound[cnt] = bound[cnt - 1] + num_bunch[i]; 28 cnt ++; 29 } 30 scanf("%d", &juicy); 31 for(int i=0; i<juicy; i++) 32 { 33 scanf("%d", &target); 34 int answer = find(target, bound, pile); 35 printf("%d\n", answer); 36 } 37 38 return 0; 39 }