题意:找到任意一个子区间使其和的绝对值最接近t
分析:1.这里一个不太好的地方就是没有给出询问t的数目,实测t不算很大,一个一个算便可以了
2.前缀和的思想,这道题好在需要对前缀和排序,使其在遍历时具有单调性,便可以用单调队列的思想做这个
题
3.这个题的细节地方比较多,需要注意什么时候要取小于等于或小于.....
总结:好久都没有做出来这个题,偷看别人排序的提示才想出来,看来序列单调性的作用不容小视啊
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL,int>pii;
#define MAXN 100005
#define x first
#define y second
#define ABS(i) ((i) < 0 ? (-(i)) : (i))
pii s[MAXN];
int n,k;
void enquiry(LL cur)
{
LL minn = 1000000001,ans;
int l,r;
for(int i = 1,j = 0;i <= n;i++)
{
while(j + 1 < i && ABS(s[i].x - s[j].x - cur) >= ABS(s[i].x - s[j + 1].x - cur))
j++;
if(ABS(s[i].x - s[j].x - cur) < minn)
{
minn = ABS(s[i].x - s[j].x - cur);
ans = s[i].x - s[j].x;
r = max(s[i].y,s[j].y);
l = min(s[j].y,s[i].y) + 1;
if(!minn)break;
}
}
printf("%lld %d %d\n",ans,l,r);
}
int main()
{
while(scanf("%d%d",&n,&k) && (n | k))
{
s[0] = pii(0,0);
for(int i = 1;i <= n;i++)
{
scanf("%lld",&s[i].x);
s[i].x = s[i].x + s[i - 1].x;
s[i].y = i;
}
sort(s,s + n + 1);
int cur;
while(k--)
{
scanf("%d",&cur);
enquiry((LL)cur);
}
}
}