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 a1,a2,…,an
.
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 a1,a2,…,an
, 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.
Input
The first line of input data contains a single integer t
(1≤t≤104
) — 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⋅105
) — the total number of matryoshkas that were in all sets.
The second line of each test case contains n
integers a1,a2,…,an (1≤ai≤109
) — the sizes of the matryoshkas.
It is guaranteed that the sum of values of n
over all test cases does not exceed 2⋅105
.
Output
For each test case, print one integer k
— the minimum possible number of matryoshkas sets.
题解:
本题重点在于一套套娃尺寸为连续的正整数
我们创建一个集合,集合插入当前尺寸s以及要组成一套套娃接下来的尺寸s+1,为避免重复查找以及寻找最小集合数目。我们用set创建一个有序列无重复的集合。接下来遍历集合,设当前尺寸套娃数量为x,访问前一个套娃尺寸数量为y;当x>y时说明该尺寸除了与前一个尺寸组成集合还能组成(x-y)个集合,如果y<x说明当前尺寸已经加入到集合数目当中
# include <bits/stdc++.h>
using namespace std;
set<int>b;
const int N=2*1e5+10;
map<int,int>cnt;
int res;
void solve()
{
int last=0;
for(auto x:b)
{
int c=cnt[x];
res+=max(0,c-last);
last=c;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
res=0;
b.clear();
cnt.clear();
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
b.insert(x);
b.insert(x+1);
cnt[x]++;
}
solve();
cout<<res<<endl;
}
}