pku3663 Costume Party
http://poj.org/problem?id=3663
二分查找
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int a[20020]; 5 6 int bs(int l, int r, int v) 7 { 8 int m; 9 while(l < r) 10 { 11 m = (l + r) >> 1; 12 if(a[m] < v) 13 { 14 l = m + 1; 15 } 16 else 17 { 18 r = m; 19 } 20 } 21 return l; 22 } 23 24 int cmp0(const void *a, const void *b) 25 { 26 return *(int *)a - *(int *)b; 27 } 28 29 int main() 30 { 31 int n, s; 32 int i, count = 0; 33 int temp; 34 scanf("%d%d", &n, &s); 35 for(i=1; i<=n; i++) 36 { 37 scanf("%d", a+i); 38 } 39 qsort(a+1, n, sizeof(a[0]), cmp0); 40 a[n+1] = (1<<31) - 1; 41 for(i=1; i<=n; i++) 42 { 43 if(a[i] < s) 44 { 45 temp = bs(i+1, n+1, s-a[i]+1); 46 count += temp-i-1; 47 //printf("%d %d\n", i, temp-i-1); 48 } 49 } 50 printf("%d\n", count); 51 return 0; 52 }