发布于 

【CF1203E】Boxers 题解

Codeforces Round #579 (Div. 3) E题

题目大意

给定你nn 个数,你可以对每个数加一或减一或不变,求使最后序列不同数最多的个数。

核心思路

nn个数排序,然后从小到大对每个数优先减一,再不济就不变,再不济就加一。这是因为,如果你优先加一很有可能就会错过最小的数减一这个答案。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
#define REP(i,e,s) for(register int i=e; i<=s; i++)
#define DREP(i,e,s) for(register int i=e; i>=s; i--)
#define DE(...) fprintf(stderr,__VA_ARGS__);
#define DEBUG(a) DE("DEBUG: %d\n",a)
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define ll long long

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

const ll INF=0x3f3f3f,MAXN=150000+10;

int a[MAXN],have[MAXN];

int main() {
int n=read();
REP(i,1,n) a[i]=read();
sort(a+1,a+1+n);
int ans=0;
REP(i,1,n) {
if(have[a[i]-1]||a[i]-1<=0) {
if(have[a[i]]) {
if(have[a[i]+1]) continue;
else have[a[i]+1]=1,ans++;
}
else have[a[i]]=1,ans++;
}
else have[a[i]-1]=1,ans++;
}

printf("%d\n",ans);
return 0;
}