您的位置:首页 > 其它

pku2153 Rank List

2013-05-29 10:18 232 查看
http://poj.org/problem?id=2153

二分查找

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <map>

using namespace std;

int n, m;
map<string, int> map1;
int count1[10010] = {0};
int sort1[10010];

string ctos(char s[])
{
int i;
string r;
for(i=0; s[i]; i++)
{
r += s[i];
}
return r;
}

int cmp(const void *a, const void *b)
{
return *((int *)b) - *((int *)a);
}

int bs(int l, int h, int v)
{
int m;
while ( l < h )
{
m = ( l + h ) >> 1;
if (sort1[m] > v)
{
l=m+1;
}
else
{
h=m;
}
}
return l;
}

int main()
{
int i, j, x, y, k;
char s[33] = "\0";
string s1;
scanf("%d%*c", &n);
for(i=1; i<=n; i++)
{
gets(s);
s1 = ctos(s);
map1.insert(make_pair(s1, i));
memset(s, 0, sizeof(s));
}
scanf("%d", &m);
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d%*c", &x);
gets(s);
s1 = ctos(s);
y = map1[s1];
//printf("%d %d\n", x, y);
count1[y] += x;
}
for(j=1; j<=n; j++)
{
sort1[j] = count1[j];
}
k = sort1[map1["Li Ming"]];
qsort(sort1+1, n, sizeof(sort1[0]), cmp);
printf("%d\n", bs(1, n+1, k));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: