ABC 272 E - Add and Mex(数学&mex)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABC 272 E - Add and Mex(数学&mex)相关的知识,希望对你有一定的参考价值。

ABC 272 E - Add and Mex(数学&mex)

显然 [ 0 , n ] [0,n] [0,n]的数才有意义。

对于 i = 1 i=1 i=1,显然有意义的数最多 n i \\dfracni in个。

求和一下最多 n log ⁡ n n\\log n nlogn个有意义的数,考虑开 m m m个vector存每轮有意义的数。

然后暴力找即可,因为这些有意的数是不同的,所以暴力最多找一次 O ( n ) O(n) O(n)找满,然后后面都是 O ( 1 ) O(1) O(1)

所以总复杂度; O ( n log ⁡ n ) O(n\\log n) O(nlogn)

#include <bits/stdc++.h>
using namespace std;

int main() 
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(auto &v : a) cin >> v;
    vector<vector<int>> vals(m + 1);
    for(int i = 0; i < n; i++) 
        if(a[i] >= n) continue;
        int l = (a[i] >= 0 ? 1 : (-a[i] + i) / (i + 1));
        int r = min(m + 1, (n - a[i] + i) / (i + 1));
        for(int j = l; j < r; j++) 
            vals[j].push_back(a[i] + (i + 1) * j);
        
    
    for(int i = 1; i <= m; i++) 
        int sz = vals[i].size();
        vector<bool> exi(sz + 1);
        for(auto v : vals[i]) 
            if(v < sz) exi[v] = true;
        
        int res = 0;
        while(exi[res]) res++;
        cout << res << endl;
    


以上是关于ABC 272 E - Add and Mex(数学&mex)的主要内容,如果未能解决你的问题,请参考以下文章

ABC 272 E - Add and Mex(数学&mex)

ABC222 E - Red and Blue Tree(思维+dp)

Codeforce842D Vitya and Strange Lesson

包括 matlab_add_mex 和 Cmake 的问题

[matlab][原创]安装mingw后调用C++进行mex加操作

B - Alyona and mex