Smallest Difference Gym - 101853J

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 个数,要求这 n 个数任意两个数之间的差的绝对值 小于等于 1, 求 n 最大。

思路:尺取。

代码

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d",a);
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;//1e10
const int mod = 1e9 + 7;
const int Maxn = 1e6+9;
const double pi = acos(-1.0);
const double eps = 1e-8;

int a[Maxn];

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        FOR(i, 1, n) cin >> a[i];
        sort(a + 1, a + 1 + n);
        int l = 1, r = 1, ans = 0;
        while (l <= r) { 
            while (r <= n && a[r]-a[l]<=1) {
                r++;
            }
       ans = max(r-l, ans);
l++; } cout << ans << endl; } }

 
代码2

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d",a);
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;//1e10
const int mod = 1e9 + 7;
const int Maxn = 1e6+9;
const double pi = acos(-1.0);
const double eps = 1e-8;

int a[Maxn];

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        FOR(i, 1, n) cin >> a[i];
        sort(a + 1, a + 1 + n);
        int l = 1, r = 1, ans = 0;
        while(1){
            while (r <= n && a[r]-a[l]<=1) {
                r++;
            }
            ans = max(r-l, ans); //要放在这个位置,不然 5 1 1 1 1 1 输出 0
            if (a[r] - a[l] <= 1)break;
            l++;
        }
        cout << ans << endl;
    }
}

 

 

posted @ 2020-04-13 13:08  AlexLIN·  阅读(276)  评论(0)    收藏  举报