Climbing the Leaderboard

Alice is playing an arcade game and wants to climb to the top of the leaderboard. Can you help her track her ranking as she beats each level? The game uses Dense Ranking, so its leaderboard works like this:

  • The player with the highest score is ranked number  on the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

For example, four players have the scores , and . Those players will have ranks , and , respectively.

When Alice starts playing, there are already  people on the leaderboard. The score of each player  is denoted by . Alice plays for  levels, and we denote her total score after passing each level  as . After completing each level, Alice wants to know her current rank.

You are given an array, , of monotonically decreasing leaderboard scores, and another array, , of Alice's cumulative scores for each level of the game. You must print  integers. The  integer should indicate the current rank of alice after passing the  level.

大意:输入n个数,代表计分板的分数。再输入m个数,表示Alice的分数,对于每一个分数,在计分板中排名的话排的到多少?

例如:

输入 

7
100 100 50 40 40 20 10
4
5 25 50 120
输出
6
4
2
1
当m,n很大时会超时,所以思路是,将计分板上相同的分数合并后,将Alice的分数从后往前比较,因为Alice的分数是从小到大,计分板是从大往小。
#include <bits/stdc++.h>

using namespace std;

int main() {
    
    int n;
    cin >> n;
    vector<int> scores(n);
    for(int scores_i = 0; scores_i < n; scores_i++){
       cin >> scores[scores_i];
        
    }
    scores.erase(unique(scores.begin(),scores.end()),scores.end());
    
    //for(int i =0;i<scores.size();i++)
        //cout<<"scores["<<i<<"]"<<scores[i]<<endl;
    int m;
    cin >> m;
    vector<int> alice(m);
    vector<int> rank;
    int i =scores.size();
    for(int alice_i = 0; alice_i < m; alice_i++){
        int f=0;
       cin >> alice[alice_i];
       while(i>=0 && alice[alice_i]>scores[i-1]){
            i--;
           
       }
        if(i==-1)
            cout<<1<<endl;
        else if(alice[alice_i] == scores[i-1])
            cout<<i<<endl;
        else if(alice[alice_i] < scores[i-1])
            cout << i+1<<endl;
        
    }
    //for(int i =0;i<rank.size();i++)
        //cout<<rank[i]<<endl;
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值