New Year Parties(贪心)

Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year…

nn friends live in a city which can be represented as a number line. The ii-th friend lives in a house with an integer coordinate xixi. The ii-th friend can come celebrate the New Year to the house with coordinate xi−1xi−1, xi+1xi+1 or stay at xixi. Each friend is allowed to move no more than once.

For all friends 1≤xi≤n1≤xi≤n holds, however, they can come to houses with coordinates 00 and n+1n+1 (if their houses are at 11 or nn, respectively).

For example, let the initial positions be x=[1,2,4,4]x=[1,2,4,4]. The final ones then can be [1,3,3,4][1,3,3,4], [0,2,3,3][0,2,3,3], [2,2,5,5][2,2,5,5], [2,1,3,5][2,1,3,5] and so on. The number of occupied houses is the number of distinct positions among the final ones.

So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?

Input
The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of friends.

The second line contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤n1≤xi≤n) — the coordinates of the houses of the friends.

Output
Print two integers — the minimum and the maximum possible number of occupied houses after all moves are performed.

Examples
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
Note
In the first example friends can go to [2,2,3,3][2,2,3,3]. So friend 11 goes to x1+1x1+1, friend 22 stays at his house x2x2, friend 33 goes to x3−1x3−1 and friend 44 goes to x4−1x4−1. [1,1,3,3][1,1,3,3], [2,2,3,3][2,2,3,3] or [2,2,4,4][2,2,4,4] are also all valid options to obtain 22 occupied houses.

For the maximum number of occupied houses friends can go to [1,2,3,4][1,2,3,4] or to [0,2,4,5][0,2,4,5], for example.
本以为是很简单很简单的一个题,做了太久了。。
对于最小的来说,因为我们只能加一或者减一或者不动,这样如果中间一个数字有人的话,我们就让两旁的过来就好了,题目中有一个隐含条件就是范围是[1,n]的。这样我们就可以按照贪心的思想,这样去做。
对于最大的来说,也是贪心去做。具体看代码:

#include
using namespace std;

const int maxx=2e5+100;
int a[maxx],x[maxx],s[maxx];
int n;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		memset(s,0,sizeof(s));
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),x[a[i]]++,s[a[i]]++;
		int _min=0;
		for(int i=1;i<=n;i++)
		{
			if(x[i])
			{
				_min++;
				i+=2;
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(s[i]==0) continue;//这个位置没有人就直接跳过
			if(s[i-1]==0)//前面没有人就尽可能往前靠
			{
				s[i]--;
				s[i-1]++;
			}
			if(s[i]>1)//如果这个位置的人数多于1个的话,就安排人往后靠,这样位置不可能少。
			{
				s[i]--;
				s[i+1]++;
			}
		}
		int _max=0;
		for(int i=0;i<=n+1;i++) if(s[i])_max++;//统计人数
		cout<<_min<<" "<<_max<<endl;
	}
}/*10
8 8 1 1 1 2 7 7 8 4->1 1 1 2 4 7 7 8 8 8*/

努力加油a啊,(o)/~

你可能感兴趣的:(New Year Parties(贪心))