poj 2566 Bound Found

题意:找到任意一个子区间使其和的绝对值最接近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);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值