pku2153 Rank List

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 }

 

转载于:https://www.cnblogs.com/yuan1991/archive/2013/05/29/pku2153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值