J - Smallest Difference

J - Smallest Difference

You are given an array a consists of n elements, find the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1.

Input
The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.

The first line of each test case consist of an integer n (2 ≤ n ≤ 104), in which n is size of the array a

The a line follow containing n elements a1, a2, …, an (1 ≤ ai ≤ 104), giving the array a.

Output
For each test case, print a single line containing the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1.

Example
Input
2
3
1 2 3
5
2 2 3 4 5
Output
2
3
大体意思就是给定一个数组,将其中的n个数列从中取出构成一个新的数列,要求新数列的任意两项之间的绝对值的差小于1
方法一

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    int a[10010];
    scanf("%d",&t);
    int N=0;//记录输入的最大的数值
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(a,0,sizeof(a));//清空数组a
        for(int i=0;i<n;i++)
        {
            int b;
             scanf("%d",&b);
             a[b]++;//记录数组元素中值为b的个数
             if(b>N)
                N=b;//记录数组元素中最大的数值
        }
        int maxx=0;//记录最大的相邻元素的总数
        for(int i=0;i<=N;i++)
        {
            if(a[i]!=0)
                a[i]+=a[i+1];//并不使用a[i-1]是因为最小的数没有
            maxx=max(maxx,a[i]);
        }
        printf("%d\n",maxx);
    }
}

方法二

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
    int t,a[10010];
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        int n,maxx=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        for(int i=0;i<n;i++)
        {
            int num=0;
            for(int j=i;j>=0;j--)
            {
                 if(a[i]-a[j]<=1)
                    num++;
                else break;
            }
            maxx=max(maxx,num);
        }
        printf("%d\n",maxx);
    }
}

此方法是直接计算有多少数字满足条件。
方法三

#include<bits/stdc++.h>
using namespace std;
int a[10050];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i = 0; i < n; i ++) 
            scanf("%d",&a[i]);
        sort(a,a + n);
        int l = 0,r = 0;
        int maxx = -1;
        for(int r = 0; r < n; r ++)
        {
            while(a[r] - a[l] > 1)
                l ++;
            maxx = max(maxx,r - l + 1);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

方法和2类似,是记录数组元素位置,并非直接历遍每一个元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pig2687

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值