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. 关于ORACLE通过file_id与block_id定位数据库对象遇到的问题的一点思考

    在ORACLE中,我们可以通过file_id(file#)与block_id(block#)去定位一个数据库对象(object).例如,我们在10046生成的trace文件中file#=4 block ...

  2. MVC5 DB FIRST

    跟着师父一直在做codefirst的开发,最近有个新需求,就是需要人家的数据库,然后来开发,现在出现问题了.整理如下 目前有个现成的我们之前的codefirst的工程代码,我记得师父说过,根据数据库生 ...

  3. signedCookies

    var express = require('../') , request = require('supertest') , cookieParser = require('cookie-parse ...

  4. nfs服务端配置文件详解

    配置参数语法 配置文件路径 /etc/exports 可以使用#进行注释说明 一个完整的配置实例至少需要3个参数 1.被共享的目录 必须是存在的目录 2.向谁共享 可以指定主机或网络范围,支持通配符* ...

  5. virtualenv 安装不同版本的虚拟环境的办法

    废话不多说直接上代码 virtualenv -p C:\Python27\python2.exe env 上面的*.exe是你要使用的python版本的exe文件的绝对路径. 官方文档参考:http: ...

  6. Python之路-shell&计划任务

    开发脚本自动部署及监控1.编写脚本自动部署反向代理.web.nfs:要求: I.部署nginx反向代理三个web服务,调度算法使用加权轮询:           II.所有web服务使用共享存储nfs ...

  7. 程序员的基本功之Java集合的实现细节

    1.Set集合与Map 仔细对比观察上面Set下和Map下的接口名,不难发现它们如此的相似,必有原因 如果只考察Map的Key会发现,它们不可以重复,没有顺序,也就是说把Map的所有的Key集中起来就 ...

  8. 大数据测试之hadoop集群配置和测试

    大数据测试之hadoop集群配置和测试   一.准备(所有节点都需要做):系统:Ubuntu12.04java版本:JDK1.7SSH(ubuntu自带)三台在同一ip段的机器,设置为静态IP机器分配 ...

  9. MongoDB--在windows下的安装过程及基本配置

    这几天在做一个简单的后台博客系统,数据库用到了 MongoDB ,虽说官方的文档比较全,但是对于我一个英语一般的人来说,或多或少在配置的时候出现了一些问题,总结了一下在安装及创建服务的过程 Mongo ...

  10. jmeter读取文件进行参数化时随机取值实现

    jmeter能用来做参数化的组件有几个,但是都没有随机取值的功能,遇到随机取值的需求怎么办呢? 突发奇想,可以用函数_StringFromFile来实现,先来介绍下此函数: 此函数位于函数助手中, 函 ...