| 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;
}
本文介绍了一种算法,用于从给定的整数序列中找出一个连续子序列,使其和的绝对值与目标值T的差最小。通过排序前缀和并使用尺取法,文章详细解释了如何高效地找到满足条件的子序列及其对应的起始和结束位置。
359

被折叠的 条评论
为什么被折叠?



