POJ 2566 Bound Found(前缀和+尺取法)

本文介绍了一种算法,用于从给定的整数序列中找出一个连续子序列,使其和的绝对值与目标值T的差最小。通过排序前缀和并使用尺取法,文章详细解释了如何高效地找到满足条件的子序列及其对应的起始和结束位置。
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6811 Accepted: 2202 Special Judge

Description

Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We'll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t. 

You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.

Input

The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n=k=0. Otherwise, 1<=n<=100000 and there follow n integers with absolute values <=10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0<=t<=1000000000.

Output

For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.

Sample Input

5 1
-10 -5 0 5 10
3
10 2
-9 8 -7 6 -5 4 -3 2 -1 0
5 11
15 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 100
0 0

Sample Output

5 4 4
5 2 8
9 1 1
15 1 15
15 1 15

题意:

给你一个包含n(n<=1e5)个数的无序序列,再给你有一个数T,让你挑出任意一个连续子序列,使得这段连续子序列的和的绝对值和T的差尽量小。输出这段连续子序列的和的绝对值,和其子序列对应下标的起始、结束位置。

思路:这道题题意有些坑,未说明输出任意答案导致我没过样例调了半天。

考虑到不能直接对数列排序,我们对前缀和进行排序,任意两个前缀和即对应一段连续的区间,然后就可以进行尺取了。

尺取的时候需要注意,只有一个前缀和构成答案的情况需要特判。

代码:

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=200010;
int n,m,k;
int a[maxn],ans;
struct node
{
    int val;
    int id;
    bool operator<(node aa)const
    {
        return val<aa.val;
    }
}sum[maxn];
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        if(!n&&!k) break;
        sum[0].val=0;
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            sum[i+1].val=sum[i].val+x;
            sum[i+1].id=i+1;
        }
        sort(sum+1,sum+1+n);
        sum[0].id=0;
        while(k--)
        {
            scanf("%d",&m);
            //cout<<m<<" * "<<endl;
            int l=1;
            int ans=inf,ansl=n,ansr=1,cha=inf;
            for(int r=1;r<=n;r++)
            {
                int tmp=abs(sum[r].val);
                //if(sum[r].id==4) cout<<sum[r].val-sum[l].val<<endl;
                int l1=0;
                int r1=sum[r].id;
                if(abs(m-tmp)<cha)
                {
                    ans=tmp;
                    cha=abs(m-tmp);
                    ansl=l1+1;
                    ansr=r1;
                }
                //if(sum[r].id==4) cout<<l<<r<<" "<<tmp<<" "<<m<<endl;
                while(l<r&&sum[r].val-sum[l].val>m)
                {
                    int tmp=sum[r].val-sum[l].val;
                    //cout<<tmp<<endl;
                    //if(sum[r].id==4) cout<<sum[r].val-sum[l].val<<endl;
                    int l1=min(sum[r].id,sum[l].id);
                    int r1=max(sum[r].id,sum[l].id);
                    if(l1>r1) swap(l1,r1);
                    if(abs(m-tmp)<cha)
                    {
                        ans=tmp;
                        cha=abs(m-tmp);
                        ansl=l1+1;
                        ansr=r1;
                    }
                    l++;
                }
                if(l==r) continue;
                tmp=sum[r].val-sum[l].val;
                l1=min(sum[r].id,sum[l].id);
                r1=max(sum[r].id,sum[l].id);
                if(l1>r1) swap(l1,r1);
                if(abs(m-tmp)<cha)
                {
                    ans=tmp;
                    cha=abs(m-tmp);
                    ansl=l1+1;
                    ansr=r1;
                }
            }
            printf("%d %d %d\n",ans,ansl,ansr);
        }
    }
    return 0;
}

 

【负荷预测】基于VMD-CNN-LSTM的负荷预测研究(Python代码实现)内容概要:本文介绍了基于变分模态分解(VMD)、卷积神经网络(CNN)和长短期记忆网络(LSTM)相结合的VMD-CNN-LSTM模型在负荷预测中的研究与应用,采用Python代码实现。该方法首先利用VMD对原始负荷数据进行分解,降低序列复杂性并提取不同频率的模态分量;随后通过CNN提取各模态的局部特征;最后由LSTM捕捉时间序列的长期依赖关系,实现高精度的负荷预测。该模型有效提升了预测精度,尤其适用于非平稳、非线性的电力负荷数据,具有较强的鲁棒性和泛化能力。; 适合人群:具备一定Python编程基础和深度学习背景,从事电力系统、能源管理或时间序列预测相关研究的科研人员及工程技术人员,尤其适合研究生、高校教师及电力行业从业者。; 使用场景及目标:①应用于日前、日内及实时负荷预测场景,支持智慧电网调度与能源优化管理;②为研究复合型深度学习模型在非线性时间序列预测中的设计与实现提供参考;③可用于学术复现、课题研究或实际项目开发中提升预测性能。; 阅读建议:建议读者结合提供的Python代码,深入理解VMD信号分解机制、CNN特征提取原理及LSTM时序建模过程,通过实验调试参数(如VMD的分解层数K、惩罚因子α等)优化模型性能,并可进一步拓展至风电、光伏等其他能源预测领域。
【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)内容概要:本文研究了一种基于融合鱼鹰和柯西变异的麻雀优化算法(OCSSA)优化变分模态分解(VMD)参数,并结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)的轴承故障诊断模型。该方法利用西储大学轴承数据集进行验证,通过OCSSA算法优化VMD的分解层数K和惩罚因子α,有效提升信号去噪与特征提取能力;随后利用CNN提取故障特征的空间信息,BiLSTM捕捉时间序列的长期依赖关系,最终实现高精度的轴承故障识别。整个流程充分结合了智能优化、信号处理与深度学习技术,显著提升了复杂工况下故障诊断的准确性与鲁棒性。; 适合人群:具备一定信号处理、机器学习及MATLAB编程基础的研究生、科研人员及从事工业设备故障诊断的工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选择的问题,实现自适应优化;②构建高效准确的轴承故障诊断模型,适用于旋转机械设备的智能运维与状态监测;③为类似机电系统故障诊断提供可借鉴的技术路线与代码实现参考。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点关注OCSSA算法的设计机制、VMD参数优化过程以及CNN-BiLSTM网络结构的搭建与训练细节,同时可尝试在其他故障数据集上迁移应用以加深理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值