Codeforces 1283 E. New Year Parties

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
Note
In the first example friends can go to [2,2,3,3]. So friend 1 goes to x1+1, friend 2 stays at his house x2, friend 3 goes to x3−1 and friend 4 goes to x4−1. [1,1,3,3], [2,2,3,3] or [2,2,4,4] are also all valid options to obtain 2 occupied houses.

For the maximum number of occupied houses friends can go to [1,2,3,4] or to [0,2,4,5], for example.

题意
给你一个序列,序列点值代表在只能这个点或它点左右,求一定变换之后最大最小值。

思路
贪心。
求最大值:开一个set,先左再中后右,如果没有就放进去,最后set的size就是答案
求最小值:开一个set,从小到大遍历,如果这个值出现了,它后面两个都可以转移在一起,直接跳过,最后set的size就是答案。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 2e5 + 5;
int Case,n;
int vv[N];
struct node{
	//int vis;
	int a;
}zr[N];
bool cmp(node a,node b){
	return a.a<b.a;
}
set<int> s1,s2;
int main(){
	Case=1;
	//input(Case);
	while(Case--){
		input(n);
		for(int i=1;i<=n;i++){
			input(zr[i].a),vv[zr[i].a]++;
		}
		sort(zr+1,zr+n+1,cmp);
		for(int i=1;i<=n;i++){
			if(s1.count(zr[i].a-1)==0){
				s1.insert(zr[i].a-1); 
			}
			else if(s1.count(zr[i].a)==0){
				s1.insert(zr[i].a); 
			}
			else if(s1.count(zr[i].a+1)==0){
				s1.insert(zr[i].a+1);
			}
		
		}
		for(int i=1;i<=n;i++){
			if(vv[i]){
				i+=2;
				s2.insert(i);
			}
		}
		//for(auto i:s1) cout<<i<<endl;
		printf("%d %d",s2.size(),s1.size());
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值