codeforces1790D

这是一道关于计算最少套娃集合数量的编程题目。给定一系列套娃尺寸,每套套娃尺寸是连续的正整数,任务是找出能形成这些尺寸的最小套娃集合数。程序通过创建集合和映射来处理数据,计算每个尺寸的出现次数并更新结果。
摘要由CSDN通过智能技术生成

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.

题解:

  1. 本题重点在于一套套娃尺寸为连续的正整数

  1. 我们创建一个集合,集合插入当前尺寸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;
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值