尺取法 J - Smallest Difference

尺取法

尺取法核心思路
尺取法其实也是一种模拟,是解决寻找区间和问题的一种方法。

假如有这么一个问题:给你一些数,请在这些数中找到一个区间,使得区间里每一个元素的和大于或等于给定的某个值。

不会尺取法的话,肯定就会开双重循环,枚举区间起点和终点,然后每一次都求一次和,再和给定的数作比较。

尺取法与它的思路类似,都是寻找一个区间的起点和终点。做法是:

用两个指针,最初都指向,这一组数中的第一个,然后如果这个区间的元素之和小于给定的数,就把右指针向右移,直到区间和大于等于给定的值为止。之后把左指针向右移,直到区间和等于给定的值为止,保存方案,继续操作。

假如左指针指向这些数的第一个,并且右指针指向这组数的最后一个,这种情况下的子区间元素之和仍然小于给定的数的话,那么就输出-1,表示不可能。

那么怎么求区间和呢?

当然,for一遍是可以的,但是太浪费时间了。我们可以引入一个累加器,初始值等于这组数中的第一个元素(因为最开始左指针和右指针都指向它),当右指针向右移时,累加器每次就加上右指针指向的元素的值。当左指针向右移时,累加器每次就减去左指针指向的值。

题目: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

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int p[11111];
        int i;
        for(i=0; i<n; i++)
        {
            cin>>p[i];
        }
        sort(p,p+n);
        int maxn=-1;
        int l=0,r=0;
        for(r=0; r<n; r++)
        {
            while(p[r]-p[l]>1)
            {
                l++;
            }
            maxn=max(maxn,r-l+1);
        }
        printf("%d\n",maxn);
    }
    return 0;
}

我学到尺取法的原址:
https://www.cnblogs.com/opbnbjs/p/9503797.html
另附模板题:
poj3061

下面附上代码(不是我写的)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int a[100000+33];
int ans,S,N;

void solve()
{
    ans=100000+44; 
    int L=1,R=1,sum=0,lsub=0;
    while(R<=N)
    {
        while(sum<S&&R<=N)//R总指向当前满足要求区间的下一个 注意此处R可能>N 
        {
            sum+=a[R];//累加器加上右指针指向的元素
            ++R;//右指针向右移
            ++lsub;
        }
        while(sum>=S)//L总指向当前区间的最左边  左闭右开 
        {
            sum-=a[L];//累加器减去左指针指向的元素的值
            ++L;//左指针右移
            --lsub;
        }
        ans=min(ans,lsub+1);
    }
}

int main()
{
    int T,i,sum;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&N,&S);
        sum=0; bool flag=false;
        for(i=1;i<=N;++i) 
        {
            scanf("%d",&a[i]);
            sum+=a[i];
            if(sum>=S&&!flag) flag=true;    
        }
        if(!flag)
        {
            printf("0\n"); continue;
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值