| Time Limit: 5000MS | Memory Limit: 65536K | |||
| Total Submissions: 1732 | Accepted: 574 | Special Judge | ||
Description
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
Output
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
Source
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 100005
int n, k;
int t;
int a[maxn];
struct node
{
int val;
int id;
bool operator < (const node &a) const
{
return val < a.val;
}
} pre[maxn];
int main()
{
while(scanf("%d%d", &n, &k)!=EOF){
if(n+k==0) break;
for(int i = 1; i <= n; i++)
scanf("%d", a+i);
pre[0].val = 0, pre[0].id = 0;
for(int i = 1; i <= n; i++)
pre[i].val = pre[i-1].val+a[i], pre[i].id = i;
sort(pre, pre+n+1);
for(int i = 0; i < k; i++){
scanf("%d", &t);
int ll, rr, val, l, r;
l = 0, r = 1, val =-(0x3f3f3f3f);
while(l<r && r <= n){
int sum = pre[r].val-pre[l].val;
if(abs(sum-t) < abs(val-t)){
val = sum;
ll = l;
rr = r;
}
if(sum>=t){
l++;
if(l == r)
r++;
}
else if(r == n)
l++;
else
r++;
}
printf("%d %d %d\n", val, min(pre[ll].id, pre[rr].id)+1, max(pre[ll].id, pre[rr].id));
}
}
}
574



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



