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
题意:找连续的数使得和的绝对值和给定数差最小
题解:想了很久没想到,****因为尺取法必须单调数列才行,而且是求连续和****,先求前缀和,给其排序,再进行尺取
因为s不能等于t,当s==t时,t要++;sum>k时,向后移动s++,反之t++;
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int v,id;
}a[N];
int n; bool comp(const edge &a,const edge &b)
{
return a.v<b.v;
}
void solve(int x)
{
int s=,t=,ss,tt,sum,ans,minn=inf;
while(s<=n&&t<=n&&minn!=){
sum=a[t].v-a[s].v;
if(abs(sum-x)<minn)
{
minn=abs(sum-x);
ans=sum;
ss=a[s].id;
tt=a[t].id;
}
if(sum>x)s++;
else if(sum<x)t++;
else break;
if(t==s)t++;
}
if(ss>tt)swap(ss,tt);
cout<<ans<<" "<<ss+<<" "<<tt<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int k,s;
while(cin>>n>>k,n||k){
a[].v=a[].id=;
for(int i=;i<=n;i++)
{
int p;
cin>>p;
a[i].v=a[i-].v+p;
a[i].id=i;
}
sort(a,a+n+,comp);
while(k--){
cin>>s;
solve(s);
}
}
return ;
}

poj2566尺取变形的更多相关文章

  1. POJ:2566-Bound Found(尺取变形好题)

    Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...

  2. Gym 100703I---Endeavor for perfection(尺取)

    题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...

  3. NOJ 1072 The longest same color grid(尺取)

    Problem 1072: The longest same color grid Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit in ...

  4. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  5. Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

    题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...

  6. poj2100还是尺取

    King George has recently decided that he would like to have a new design for the royal graveyard. Th ...

  7. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  8. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  9. cf1121d 尺取

    尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...

随机推荐

  1. UML软件方法大纲

    利用周末的时间读了潘加宇的<软件方法(上)>,希望梳理清楚UML的知识脉络: 工作流 子流程 内容 备注 建模和uml   利润=需求-设计   愿景   缺乏清晰.共享的愿景往往是项目失 ...

  2. Java 中的数组

    1.声明数组String [] arr;int arr1[];String[] array=new String[5];int score[]=new int[3]; 2.初始化数组://静态初始化i ...

  3. android国际化

    语言的国际化 为了提供不同语言的版本,只需要在res中新建几个values文件夹就行 不过文件夹有自己的命名规则 values-语言代码-r国家或者地区的代码 然后我们只需要将不同语言的string. ...

  4. boost.asio源码阅读(1) - 从chat_server开始

    1. 关于示例代码 chat 先从简单的入手, 在如下路径:boost_1_63_0/libs/asio/example/cpp11/chat中找到chat_server.cpp 查看其成员, pri ...

  5. webpack学习笔记(二)-- 初学者常见问题及解决方法

    这篇文章是webpack学习第二篇,主要罗列了本人在实际操作中遇到的一些问题及其解决方法,仅供参考,欢迎提出不同意见. 注:本文假设读者已有webpack方面相关知识,故文中涉及到的专有名词不做另外解 ...

  6. Java基础二:常量池

    目录: 自动装箱与拆箱 常量池 ==与equals()区别 1. 自动装箱与拆箱 Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成 ...

  7. Jmeter-线程组

    1.Sampler 取样器(Sampler)是性能测试中向服务器发送请求,记录响应信息,记录响应时间的最小单元,JMeter 原生支持多种不同的sampler , 如 HTTP Request Sam ...

  8. 【Java基础】 Java动态代理机制

    在Java的动态代理机制中,有两个重要的类.一个是InvocationHandler,另一个是Proxy. InvocationHandler:每一个动态代理类都必须要实现InvocationHand ...

  9. DPDK QoS之分层调度器

    原创翻译,转载请注明出处. 分层调度器的时机主要体现在TX侧,正好在传递报文之前.它的主要目的是在每个网络节点按照服务级别协议来对不同的流量分类和对不同的用户的报文区分优先级并排序. 一.概述分层调度 ...

  10. python之smtplib发邮件

    第一版: 认证发信,不支持附件 #!/usr/bin/env python # --------------------------------------- # author : Geng Jie ...