E. Boxers
There are n boxers, the weight of the i-th boxer is ai. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.
It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers’ weights in the team are different (i.e. unique).
Write a program that for given current values ai will find the maximum possible number of boxers in a team.
It is possible that after some change the weight of some boxer is 150001 (but no more).
Input
The first line contains an integer n (1≤n≤150000) — the number of boxers. The next line contains n integers a1,a2,…,an, where ai (1≤ai≤150000) is the weight of the i-th boxer.
Output
Print a single integer — the maximum possible number of people in a team.
Examples
inputCopy
4
3 2 4 1
outputCopy
4
inputCopy
6
1 1 1 4 4 4
outputCopy
5
Note
In the first example, boxers should not change their weights — you can just make a team out of all of them.
In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5,4,3,2,1.
正常人做法
int n,m;cin>>n;
int minn=1e9,maxn=0,ans=0;
for(int i=0;i<n;++i)cin>>m,++a[m],minn=min(minn,m),maxn=max(maxn,m);
for(int i=max(1,minn-1);i<=maxn+1;++i)
if(a[i-1])++ans;
else if(a[i])++ans,--a[i];
else if(a[i+1])++ans,--a[i+1];
cout<<ans;
无聊试一下网络流也能过就是太慢
每个数出现一次:每个数x向汇点T连容量1的边
每个数变化<=1:每个数x向x-1,x,x+1连容量1的边
源点S向读入的数x连容量1的边
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace NetFlow{
const int MAXN=2e6,MAXM=2e6,INF=0x3f3f3f3f;//点数/边数
struct Edge{
int to,next,cap,flow;
}edge[MAXM];
int tol,head[MAXN],gap[MAXN],dep[MAXN],cur[MAXN],Q[MAXN],S[MAXN];
void init(){tol=0;memset(head,-1,sizeof(head));}
void link(int u,int v,int w){
edge[tol]={v,head[u],w,0},head[u]=tol++;
edge[tol]={u,head[v],0,0},head[v]=tol++;
}
void BFS(int start,int end){
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=1,dep[end]=0;
int front=0,rear=0;
Q[rear++]=end;
while(front!=rear){
int u=Q[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dep[v]!=-1)continue;
Q[rear++]=v,dep[v]=dep[u]+1;
++gap[dep[v]];
}
}
}
int ISAP(int start,int end,int N){//源点/汇点/点数
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top=0,u=start,maxflow=0;
while(dep[start]<N){
if(u==end){
int Min=INF,inser;
for(int i=0;i<top;++i)if(Min>edge[S[i]].cap-edge[S[i]].flow)
Min=edge[S[i]].cap-edge[S[i]].flow,inser=i;
for(int i=0;i<top;++i)edge[S[i]].flow+=Min,edge[S[i]^1].flow-=Min;
maxflow+=Min,top=inser,u=edge[S[top]^1].to;
continue;
}
bool flag=false;int v;
for(int i=cur[u];i!=-1;i=edge[i].next){
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
flag=true,cur[u]=i;break;
}
}if(flag){
S[top++]=cur[u];
u=v;continue;
}
int Min=N;
for(int i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
Min=dep[edge[i].to],cur[u]=i;
--gap[dep[u]];
if(!gap[dep[u]])return maxflow;
dep[u]=Min+1;++gap[dep[u]];
if(u!=start)u=edge[S[--top]^1].to;
}
return maxflow;
}
}using namespace NetFlow;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;init();
int maxn=0,minn=1e9;
for(int i=0;i<n;++i){
int tmp;cin>>tmp;
maxn=max(maxn,tmp),minn=min(minn,tmp);
link(0,tmp+n*2,1);
if(tmp>1)link(tmp+n*2,tmp-1,1);
link(tmp+n*2,tmp,1);
link(tmp+n*2,tmp+1,1);
}
for(int i=max(1,minn-1);i<=maxn+1;++i)link(i,n<<2,1);
cout<<ISAP(0,n<<2,n<<3);
return 0;
}