UVALive - 2678 Subsequence

本文介绍了一种寻找连续子序列使和大于等于给定值的算法问题,提供了两种解决方案,一种为nlog(n)复杂度,另一种为O(n)复杂度。
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

Description

Download as PDF

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.

Input

Many test cases will be given. 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.

Output

For each the case the program has to print the result on separate line of the output file. If there isn't such a subsequence, print 0 on a line by itself.

Sample Input

10 15 
5 1 3 5 10 7 4 9 2 8 
5 11 
1 2 3 4 5

Sample Output

2 
3

Source





分析:
解法一:
ac代码:nlog(n)
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
int a[maxn],b[maxn];
int n,s;
int main()
{
    while(scanf("%d%d",&n,&s)!=EOF)
    {
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        b[0]=0;
        for(int i=1;i<=n;i++)
        b[i]=b[i-1]+a[i];
        int ans=n+1;
        for(int j=1;j<=n;j++)
        {
            int i=lower_bound(b,b+j,b[j]-s)-b;
            if(i>0) ans=min(ans,j-i+1);
        }
        printf("%d\n",ans==n+1?0:ans);
    }
    return 0;
}

解法二:O(n)
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
int a[maxn],b[maxn];
int n,s;
int main()
{
    while(scanf("%d%d",&n,&s)!=EOF)
    {
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        b[0]=0;
        for(int i=1;i<=n;i++)
        b[i]=b[i-1]+a[i];
        int ans=n+1;
        int i=1;
        for(int j=1;j<=n;j++)
        {
            if(b[i-1]>b[j]-s) continue;
            while(b[i]<=b[j]-s) i++;
            ans=min(ans,j-i+1);
        }
        printf("%d\n",ans==n+1?0:ans);
    }
    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**(后续发展) --- 如果需要更具体的解释(例如某句话中的用法),可以补充上下文 😊
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值