首页 > 其他 > 详细

【Binary Search】474B

时间:2017-12-30 10:45:27      阅读:217      评论:0      收藏:0      [点我收藏+]

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 }

 

【Binary Search】474B

原文:https://www.cnblogs.com/Leslieeeeee/p/8147982.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!