Codeforces Round # 611 (Div. 3) E. New Year Parties (greedy)

Title links
Here Insert Picture Description
Here Insert Picture Description
that Italy: Given an initial sequence, each element of a predetermined sequence may be + 1, -1 or the same operation, the operation after a meal, the number of different elements of sequence X, and seeking the maximum of x minimum
ideas: minimization easy to think, you can re-sort to go, as long as there is a continuous and elements such as 1,2,3 difference is certainly combined 1, but there is a loophole here is that such 24 actually it can be combined together to add this condition on the line.
As for seeking the maximum, you can count the number of each of the first number, and then enumerate each number, as long as there is space left to go out into the left shift, if he still has more to go out into the right shift.

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
typedef long long ll;
int n,a[maxn],b[maxn],maxx=0,num[maxn]={0},ans;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i],maxx=max(maxx,a[i]),num[b[i]]++; 
	sort(a+1,a+1+n);
	sort(b+1,b+1+n);
	int size=unique(a+1,a+1+n)-a-1;
	int j=1;
	a[size+1]=1e6,a[size+2]=1e9;
	while(j<=size)
	{
		if(a[j+2]-a[j+1]==1&&a[j+1]-a[j]==1) j+=3;
		else if(a[j+1]-a[j]==1) j+=2;
		else if(a[j+1]-a[j]==2) j+=2;
		else j++;
		ans++;
	}
	printf("%d ",ans);
	ans=0;
	for(int i=1;i<=n+1;++i)
	{
		if(num[i]==0) continue;
		if(num[i-1]==0) num[i]--,num[i-1]++;
		if(num[i]>1) num[i]--,num[i+1]++;
	}
	for(int i=0;i<=maxx+1;++i) if(num[i]) ans++; 
	printf("%d\n",ans);
}
Published 70 original articles · won praise 0 · Views 2432

Guess you like

Origin blog.csdn.net/qq_42479630/article/details/104232781