Subsequence(尺取法)

本文介绍了一种利用尺取法解决特定问题的算法:在一系列正整数中找到和大于或等于给定值S的最短连续子序列。通过逐步增加或减少子序列的元素来调整和,直至找到满足条件的最短子序列。

题目

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

输入

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

输出

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

基本思路

本题意思为,求和大于或等于S的最小区间,根据题意可知这道题应该使用尺取法。

尺取法

尺取法顾名思义用到了“尺子”,我们可以先从第一个开始,逐个加起来与S进行比较,就像尺子一样慢慢增长,如果和大于S则我们考虑能否缩小尺子,减去最前面的数,再与S比较,每次纪录尺子的长度,所有长度中最小值及为所求。

源代码为

#include <iostream>
#include <string.h>
using namespace std;
int a[200000];
int sum;
int n,s;
void ruler()
{
    sum=0;
    int i=0;
    int r=0;
    int l=5555555;
    while(1)
    {
        while(sum<s&&i<n)
        {
            sum+=a[i++];
        }
        if(sum<s)
        {
            break;
        }
        l=min(l,i-r);
        sum-=a[r++];
    }
    if(l==5555555)
    {
        cout<<0<<endl;
        return ;
    }
    else
    {
        cout<<l<<endl;
        return ;
    }

}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        cin>>n>>s;
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
        }
        ruler();
    }
    return 0;
}
<think>好的,用户现在问的是“subsequent啥意思”,看起来是想知道这个英文单词的中文意思。先查一下这个词的基本含义。Subsequent是形容词,主要意思是“随后的,接下来的”。比如,“subsequent events”就是“随后的事件”。 接下来,我要考虑用户可能的背景。用户可能是在阅读英文材料时遇到了这个词,或者是在学习英语词汇。这时候,提供准确的翻译和例句会很有帮助。同时,用户之前的问题是关于Linux命令的,可能有一定技术背景,但这次的问题更偏向日常英语,所以需要调整回答方式,确保简洁明了。 需要确认拼写是否正确,用户写的是“subsequent”,拼写正确。接下来,要给出词性、音标、中文翻译,以及常见用法和例句。可能还需要区分近义词,比如“following”和“later”,帮助用户更好地理解不同语境下的使用。 另外,用户可能希望知道这个词在实际中的用法,比如在句子中的位置,常见的搭配,比如“subsequent to”这种介词搭配。同时,要避免使用过于复杂的解释,保持口语化,让用户容易理解。 还要注意用户之前的问题涉及技术命令,但这次是词汇问题,所以不需要联系之前的内容,专注于当前问题即可。可能需要检查是否有其他可能的含义,比如在法律或技术文档中的特殊用法,但通常subsequent的基本含义已经足够覆盖大部分情况。 最后,确保回答结构清晰,分点列出,让用户一目了然。包括发音、词性、释义、例句、近义词和常见搭配,这样用户能全面掌握这个词的用法。同时,提醒用户如果有具体语境可以进一步帮助解释,这样可以鼓励用户提供更多信息,以便更精准地解答。</think>“subsequent” 是英语中的形容词,常见含义和用法如下: --- ### **1. 基础释义** - **中文翻译**:**随后的**,**接下来的**,**之后发生的** - **音标**:英式 /ˈsʌbsɪkwənt/,美式 /ˈsʌbsɪkwənt/ - **词性**:形容词 - **反义词**:previous(之前的) --- ### **2. 常见用法** #### **(1) 描述时间顺序** 表示某事件发生在另一事件**之后**: - **例句**: - The first experiment failed, but **subsequent attempts** succeeded. (第一次实验失败了,但**随后的尝试**成功了。) - Heavy rain caused flooding, and **subsequent landslides** blocked the roads. (暴雨引发洪水,**随后的山体滑坡**堵塞了道路。) #### **(2) 搭配介词 "to"** 表示“在...之后”: - **例句**: - **Subsequent to** the meeting, the company announced new policies. (**会议结束后**,公司宣布了新政策。) --- ### **3. 近义词辨析** | 词汇 | 区别 | |--------------|--------------------------------------| | **following** | 强调紧接其后(如 "the following day" 次日) | | **later** | 泛指“后来”,时间间隔可长可短 | | **next** | 强调顺序中的“下一个”(如 "next chapter" 下一章) | --- ### **4. 技术/学术场景中的用法** - **法律文件**:指条款或条件的后续修订(如 "subsequent amendments" 后续修正案)。 - **科学研究**:描述实验、观察的后续阶段(如 "subsequent analysis" 后续分析)。 --- ### **5. 常见搭配** - **subsequent events**(后续事件) - **subsequent chapters**(后续章节) - **subsequent generations**(后代) - **subsequent development**(后续发展) --- 如果需要更具体的解释(例如某句话中的用法),可以补充上下文 😊
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值