zoukankan      html  css  js  c++  java
  • CodeForces 1294D MEX maximizing(思维)

    http://codeforces.com/contest/1294/problem/D

     

     

    大致题意:

    刚开始有一个空集合,会往里添加q次数,每次加一个值,而且你可以让这个数任意加减x若干次

    每次添加后就查询当前最小的不属于这个集合的非负整数是什么。尽可能让这个最小的不属于这个数列的非负整数最大。

    解题思路:

    由于每次添加的数t可以加减任意次的x,故我们只需记录t%x,用num[i]表示i的个数

    用ans去递增查询是否可以满足要求就行,如果num[ans%x]不为0,说明之前有一个没发挥作用的t可以用来顶替该位置上的ans,ans就可以+1去查询下一个

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const int maxn=1e5+10;
    17 using namespace std;
    18 
    19 int num[400010];
    20 
    21 int main()
    22 {
    23     #ifdef DEBUG
    24     freopen("sample.txt","r",stdin);
    25     #endif
    26 //    ios_base::sync_with_stdio(false);
    27 //    cin.tie(NULL);
    28     
    29     int q,x;
    30     scanf("%d %d",&q,&x);
    31     int ans=0;
    32     while(q--)
    33     {
    34         int t;
    35         scanf("%d",&t);
    36         num[t%x]++;
    37         while(num[ans%x])
    38         {
    39             num[ans%x]--;//有一个t变成ans的值加入集合中 
    40             ans++;//去查询下一个 
    41         }
    42         printf("%d
    ",ans);
    43     }
    44     
    45     return 0;
    46 }

    -

  • 相关阅读:
    websocketpp相关
    大地水准面、大地基准面
    ubuntu18.04 和 qt 5.13.1 安装
    高斯——克吕格投影反算
    高斯——克吕格投影正算
    缓和曲线10麦克康奈尔
    vsCode 需安装的扩展
    显示windows电脑上已连接过的wifi密码
    linux Java项目CPU内存占用高故障排查
    tcpdump常用参数说明及常见操作
  • 原文地址:https://www.cnblogs.com/jiamian/p/12230662.html
Copyright © 2011-2022 走看看