CodeForces-1283E. New Year Parties (greedy)

Main idea:

Each number of n numbers has a position x[i]. You can operate on each number and move it to x[i]+1 or x[i]-1 or just
ask you the most occupied position and the least occupied position. position

Question idea:

When seeking the most positions: after
we sort the x array, the positional relationship is increasing.
For the current x[i] if x[i]-1 does not exist, we give priority to filling x[i]-1
because x[i] ] Increases x[i]-1 this position will no longer be able to move to.
If x[i]-1 exists, then fill x[i] second priority
for the same reason as above when
seeking the least position:
for the sequence 4 4 ​​5 5 6 7 8 When 9 9 is the
minimum, just consider it as 4 5 6 7 8 9
because a number can cover the position of +1 or -1,
so directly enumerate a[i] as seven points, then as long as it is less than or equal to a[i]+ 2 are covered.
After exceeding, create a new starting point ans2++

Code:

int n,a[maxn],num[maxn],b[maxn],cnt,ans2,ans1,dp[maxn],vis[maxn];
int main() {
    
    
	n=read();
	rep(i,1,n) a[i] = read(),num[a[i]]++;
	rep(i,1,n) if(num[i]) b[++cnt] = i;
	sort(a+1,a+1+n);
	int minn = a[1];
	ans1=1;
	for(int i=1  ; i<=cnt ; i++) {
    
    
		int t = b[i];
		if(abs(t-minn)<=2)	vis[t] =1 ;
		else ans1++,minn = t;
	}
	mst(vis,0);
	for(int i=1 ; i<=n ; i++) {
    
    
		int t = a[i];
		if(vis[t-1]==0) vis[t-1] =1 ,dp[i] = dp[i-1]+1;
		else if(vis[t]==0) vis[t]=1,dp[i] = dp[i-1]+1;
		else if(vis[t+1]==0) vis[t+1]=1,dp[i] = dp[i-1]+1;
		else dp[i] = dp[i-1]; 
	}
	ans2 = dp[n];
	cout<<ans1<<" "<<ans2<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/wmy0536/article/details/110964431
Recommended