Stay Hungry,Stay Foolish!

D - Diversity of Scores

D - Diversity of Scores

https://atcoder.jp/contests/abc343/tasks/abc343_d

 

思路

准备两个map

第一个存储, 每个分数作为key, 以及 得此分数的 运动员列表 作为value

  这样,可以非常快速统计出某一时刻 所有分数总数。

第二个存储, 每个运动员作为key, 以及 此运动员当前的分数 作为value

  对于在线属于,其协助结算新的分数,并更新第一个map

 

第一个map,更新过程中,可能某个分数,之前有运动员,

但是后来运动员被移除了, 需要使用erase方法消除分数key

https://www.geeksforgeeks.org/how-to-delete-key-value-pair-from-map-in-cpp/、

 

Code

int n, t;
map<LL, set<int>> sp; // score of players
map<int, LL> ps; // player score

int main()
{
    cin >> n >> t;
    
    for(int i=1; i<=n; i++){
        sp[0].insert(i);
        
        ps[i] = 0;
    }

    for(int i=0; i<t; i++){
        int a, b;
        cin >> a >> b;
        
        LL olds = ps[a];
        LL news = ps[a] + b;
        
        sp[olds].erase(a);
        if (sp[olds].size() == 0){
            sp.erase(sp.find(olds));
        }

        ps[a] = news;
        sp[news].insert(a);

        cout << sp.size() << endl;
    }

    return 0;
}

 

posted @ 2024-03-02 23:46  lightsong  阅读(9)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel