Codeforces Global Round 9 D. Replace by MEX(1900)

1375 D. Replace by MEX
在这里插入图片描述
在这里插入图片描述
题目大意:
给你一个数列的 mex 的定义:
mex = 不存在于该数列中的最小非负数

然后进行操作:用这个数列的 mex 替换掉该数列中任何元素
最后让输出 操作数 和 每次被替换的序列下标(下标从 1 开始)

题目要求:操作数 < 2n

题目给出:长度为 n 的初始数列

思路分析:
这里想法有些偷鸡,我感觉没有 YES 和 NO 的情况下,基本上操作数都能符合题目要求QWQ(后面才知道,替换一个 a[i] = i ,最多需要两次操作)

所以我的做法就是:求出数列的 mex 然后分情况,如果 mex < n,就进行替换 a[mex] = mex,以达到 a[i] = i(这样数列肯定满足非递减)如果 mex >= n,就替换掉该数列最后一个元素(依然是为了保证数列非递减)

AC代码:

#include <iostream>
#include <cstring>
 
using namespace std;
 
const int N = 1010;
const int M = 2020;
 
int a[N];
int b[N];
int n;
int c[M];
 
 // 求一个数组的 mex
int mex() {
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; i++)
        b[a[i]]++;
    for (int i = 0; i <= n; i++)
        if (b[i] == 0)
            return i;
}
 
 // 查看是否符合题意(数列非递减)
bool check() {
    for (int i = 0; i < n - 1; i++)
        if (a[i] > a[i + 1])
            return false;
    return true;
}
 
int main() {
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        int tol = 0;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        while (1) {
            int m = mex();
            if (m < n) {
                a[m] = m;
                c[tol++] = m;
            } else {
                for (int i = 0; i < n; i++)
                    if(a[i] != i) {
                        a[i] = m;
                        c[tol++] = i;
                        break;
                    }
            }
            if (check())
                break;
        }
        cout << tol << endl;
        for (int i = 0; i < tol; i++)
            cout << c[i] + 1 << ' ';
        cout << endl;
    }
    return 0;
}
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值