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

题目链接:https://codeforc.es/contest/1283/problem/E

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

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

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

For example, let the initial positions be x=[1,2,4,4]. The final ones then can be [1,3,3,4], [0,2,3,3], [2,2,5,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 n (1≤n≤2⋅105) — the number of friends.

The second line contains n integers x1,x2,…,xn (1≤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

题意

n 个人住在一个可以用数轴表示的城市里,第i个人住在一个整数坐标xi的房子里。第 i 个人可以和坐标 xi - 1 ,xi+1 一起来家里庆祝新年,或者呆在xi,每个人只能移动一次。对于房子在 1 或 n 的人,他们可以来到坐标 0 或 n + 1 的房子。
所有人可以随意选择三种操作之一,然后计算有人的房屋总数。有人的房屋可能达到的最小数量和最大数量各是多少?

分析

贪心

最小值:对于每个人,如果前面已经有人了,就往前靠,如果没有,就优先往后靠;
最大值:对于每个人,优先往前靠(如果前面没人),前面有人就看中间,中间有人就看右边。

代码
#include<bits/stdc++.h>
using namespace std;

int n;
int a[200007];
int vis[200007];
int minn,maxn;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a + 1, a + 1 + n);
	
	for(int i=1;i<=n;i++)
	{
		if(vis[a[i] + 1] || vis[a[i]] || vis[a[i] - 1]) continue;
		vis[a[i] + 1] = 1;
		minn++;
	}
	
	memset(vis, 0, sizeof(vis));
	for(int i=1;i<=n;i++)
	{
		if(vis[a[i] - 1] == 0) maxn++, vis[a[i] - 1] = 1;
		else if(vis[a[i]] == 0) maxn++, vis[a[i]] = 1;
		else if(vis[a[i] + 1] == 0) maxn++, vis[a[i] + 1] = 1;
	}
	printf("%d %d",minn,maxn);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值