Codeforces Round 977 (Div. 2, based on COMPFEST 16 - Final Round) B. Maximize Mex

题目链接:题目

大意:

给定一个数组,你可以任意给它们加上 x x x,求最大 M e x Mex Mex

思路:

容易知道,给一个元素加上 x x x只会影响后面的,另外,我们知道,只有重复多余的数才能加 x x x去填补后面的空缺。
我一开始从头到尾遍历,发现有重复的就不断加 x x x,直到不与其他数重叠,然而超时了,然后我们发现不用一个个加,只用储存它模 x x x,存到哈希表里,再访问它模 x x x就行了,这是由于加 x x x的任意性。

代码:

#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define MOD 1000000007
#define fi first
#define se second
#define pii pair<int,int>
#define vec vector
 
void solve(){   
    int n, x;
    cin >> n >> x;
    vec<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    unordered_map<int, int> mp, mp2;
    sort(a.begin(), a.end());
    for(int num : a){
        mp[num]++;
    }
    for(int i = 0; i <= n; i++){
        if(mp[i] > 1){
            mp2[i % x] += mp[i] - 1;
        }
        if(!mp[i] && mp2[i % x]){
            mp[i]++;
            mp2[i % x]--;
        }
    }
    //cout << mp[1];
    for(int i = 0; i <= n; i++){
        if(!mp[i]){
            cout << i << '\n';
            return;
        }
    }
}
 
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值