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\n",ans);
43     }
44     
45     return 0;
46 }

 

 

 

 

 

-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM