[CodeForces - 1294D] MEX maximizing

  • 题意:求当前数列中未出现的最小的数值。数列中的元素可以进行无限次数的+x或-x操作。当然我们要使得最小数值最大化。

思路

对于当前数列,我们可以得到数列[0, x - 1]的叠加。叠加次数可能为0可能为多,取决于木桶定律。

这就给了我们一个提示,也就是统计数列元素值%x的个数。

我们的数列最小值now从0开始,如果now%x存在,那么我们用该数值,++now,直到now%x不存在为止。

每次输入都输出当前now即可!


(开始写了一发n^2的暴力,想看哈能不能过,果然T了……这种思维,是看的别人博客,自己还是没想到……%%%%

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

inline int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

const int maxN = 400005;
int q, x;
int num[maxN];

int main()
{
    int now = 0;
    q = read(); x = read();
    while(q -- )
    {
        int a; a = read();
        ++num[a % x];
        while(num[now % x])
        {
            -- num[now % x];
            ++ now;
        }
        printf("%d\n", now);
    }
    return 0;
}
发布了242 篇原创文章 · 获赞 68 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/104410000
Mex
今日推荐