題意:一個人有一些考試,每次得到一個分數,問經過i次考試後總分排名第幾。
思路:map的應用,一一對應即可。
代碼:
[cpp]
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
const int N = 10010;
int main(){
//freopen("1.txt","r",stdin);
int n;
while(scanf("%d",&n) != EOF){
string ss;
getchar();
map<string,int> mp;
map<string,int> :: iterator it;
for(int i = 0; i < n; ++i){
getline(cin,ss);
mp[ss] = 0;
}
int m;
scanf("%d",&m);
getchar();
for(int i = 0; i < m; ++i){
int value = 0,score = 0;
for(int j = 0;j < n; ++j){
scanf("%d",&score);
getchar();
getline(cin,ss);
mp[ss] += score;
if(ss == "Li Ming"){
value = mp[ss];
}
}
int cnt = 0,num[N];
for(it = mp.begin(); it != mp.end(); ++it){
num[cnt++] = it -> second;
}
sort(num,num+cnt);
int lp = 0,rp = cnt - 1,ans = 0;
while(lp <= rp){
int mid = (lp + rp) / 2;
if(num[mid] <= value){
ans = mid;
lp = mid + 1;
} www.2cto.com
else
rp = mid - 1;
}
printf("%d\n",n - ans);
}
}
return 0;
}