http://poj.org/problem?id=2153
二分查找
1 #include <stdio.h> 2 #include <iostream> 3 #include <string> 4 #include <string.h> 5 #include <map> 6 7 using namespace std; 8 9 int n, m; 10 map<string, int> map1; 11 int count1[10010] = {0}; 12 int sort1[10010]; 13 14 string ctos(char s[]) 15 { 16 int i; 17 string r; 18 for(i=0; s[i]; i++) 19 { 20 r += s[i]; 21 } 22 return r; 23 } 24 25 int cmp(const void *a, const void *b) 26 { 27 return *((int *)b) - *((int *)a); 28 } 29 30 int bs(int l, int h, int v) 31 { 32 int m; 33 while ( l < h ) 34 { 35 m = ( l + h ) >> 1; 36 if (sort1[m] > v) 37 { 38 l=m+1; 39 } 40 else 41 { 42 h=m; 43 } 44 } 45 return l; 46 } 47 48 int main() 49 { 50 int i, j, x, y, k; 51 char s[33] = "\0"; 52 string s1; 53 scanf("%d%*c", &n); 54 for(i=1; i<=n; i++) 55 { 56 gets(s); 57 s1 = ctos(s); 58 map1.insert(make_pair(s1, i)); 59 memset(s, 0, sizeof(s)); 60 } 61 scanf("%d", &m); 62 for(i=1; i<=m; i++) 63 { 64 for(j=1; j<=n; j++) 65 { 66 scanf("%d%*c", &x); 67 gets(s); 68 s1 = ctos(s); 69 y = map1[s1]; 70 //printf("%d %d\n", x, y); 71 count1[y] += x; 72 } 73 for(j=1; j<=n; j++) 74 { 75 sort1[j] = count1[j]; 76 } 77 k = sort1[map1["Li Ming"]]; 78 qsort(sort1+1, n, sizeof(sort1[0]), cmp); 79 printf("%d\n", bs(1, n+1, k)); 80 } 81 return 0; 82 }