Matryoshkas
题目描述:
Matryoshka is a wooden toy in the form of a painted doll, inside which you can put a similar doll of a smaller size.
A set of nesting dolls contains one or more nesting dolls, their sizes are consecutive positive integers. Thus, a set of nesting dolls is described by two numbers: \(s\) — the size of a smallest nesting doll in a set and \(m\) — the number of dolls in a set. In other words, the set contains sizes of \(s,s+1,…,s+m−1\) for some integer \(s\) and \(m(s,m>0)\).
You had one or more sets of nesting dolls. Recently, you found that someone mixed all your sets in one and recorded a sequence of doll sizes — integers \(a_1,a_2,…,a_n\).
You do not remember how many sets you had, so you want to find the minimum number of sets that you could initially have.
For example, if a given sequence is \(a=[2,2,3,4,3,1]\). Initially, there could be \(2\) sets:
- the first set consisting of \(4\) nesting dolls with sizes \([1,2,3,4]\);
- a second set consisting of \(2\) nesting dolls with sizes \([2,3]\).
According to a given sequence of sizes of nesting dolls \(a_1,a_2,…,a_n\), determine the minimum number of nesting dolls that can make this sequence.
Each set is completely used, so all its nesting dolls are used. Each element of a given sequence must correspond to exactly one doll from some set.
输入描述:
The first line of input data contains a single integer \(t(1≤t≤10^4)\) — the number of test cases.
The description of the test cases follows.
The first line of each test case contains one integer \(n(1≤n≤2⋅10^5)\) — the total number of matryoshkas that were in all sets.
The second line of each test case contains \(n\) integers \(a_1,a_2,…,a_n(1≤a_i≤10^9)\) — the sizes of the matryoshkas.
It is guaranteed that the sum of values of \(n\) over all test cases does not exceed \(2⋅10^5\).
输出描述:
For each test case, print one integer \(k\) — the minimum possible number of matryoshkas sets.
样例:
input:
10
6
2 2 3 4 3 1
5
11 8 7 10 9
6
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
8
1 1 4 4 2 3 2 3
6
1 2 3 2 3 4
7
10 11 11 12 12 13 13
7
8 8 9 9 10 10 11
8
4 14 5 15 6 16 7 17
8
5 15 6 14 8 12 9 11
5
4 2 2 3 4
output:
2
1
6
2
2
2
2
2
4
3
Note:
The first test case is described in the problem statement.
In the second test case, all matryoshkas could be part of the same set with minimum size \(s=7\).
In the third test case, each matryoshka represents a separate set.
AC代码1:
#include <bits/stdc++.h>
using namespace std;
// 根据题意得知只需要知道连续的数字最多能有多少组就好了
// 那么可以从每个数字出现的次数入手
// 首先加上第一个数出现的次数
// 设 x 是目前判断到的数字,y 是上一个数字
// 如果 x 的大小是 y + 1, 说明是连续的那么就用 x 出现的次数减去 y 出现的次数
// 如果相减之后是大于0,则说明 x 有多出来的,多出来的 x 就会成为新的连续数字组的开头
// 如果小于0,说明 y 多,由于 y 已经被判断过了,直接加0即可
// 如果 x 的大小不是 y + 1,说明不是连续的,那么直接加上 x 出现的次数,让每个 x 成为数字组的开头
void solve()
{
int n;
cin >> n;
vector<int> a(n);
// 不要用unordered_map,会超时
map<int, int> p;
for(int i = 0; i < n; i ++)
{
cin >> a[i];
p[a[i]] ++;
}
// 排序加去重
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int l = a.size();
int ans = 0;
for(int i = 0; i < l; i ++)
{
if(i == 0 || a[i] != a[i - 1] + 1)
ans += p[a[i]];
else
ans += max(p[a[i]] - p[a[i - 1]], 0);
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T --)
solve();
return 0;
}
AC代码2:
#include <bits/stdc++.h>
using namespace std;
// 与上一个代码思路一致,用了set
void solve()
{
int n;
cin >> n;
vector<int> a(n);
set<int> s;
map<int, int> p;
for(int i = 0; i < n; i ++)
{
cin >> a[i];
s.insert(a[i]);
s.insert(a[i] + 1);
p[a[i]] ++;
}
int ans = 0;
int y = 0;
for(auto x : s)
{
ans += max(0, p[x] - p[y]);
y = x;
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T --)
solve();
return 0;
}