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

思路:这个题拿到手,很容易想到的是暴力,但是很明显,必然会超时,所以,我们要换种思路,既然是尺取题,尺取关键要单调,这是个比较难处理的过程,没想好如何去尺取处理,然后参考了一篇博客,恍然大悟,博客链接:https://blog.csdn.net/acm_cxq/article/details/51854210,很巧妙的做法,只是没想到,关键就是求和排序,这是个突破点

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define INF 0x3f3f3f3f const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll a[maxn];
ll abss(ll x) {
if(x<0) {
return -x;
} else {
return x;
}
}
struct node {
ll sum,id;
} p[maxn]; bool cmp(node x,node y) {
return x.sum<y.sum;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,k;
while(cin>>n>>k) {
if(n==0&&k==0) {
break;
}
p[0].sum=0,p[0].id=0;
for(int t=1; t<=n; t++) {
cin>>a[t];
p[t].sum=p[t-1].sum+a[t];
p[t].id=t; }
sort(p,p+n+1,cmp);
int s;
for(int t=0; t<k; t++) {
scanf("%d",&s);
ll l=0,r=1;
ll ans=INF;
ll ansl,ansr,anss;
while(r<=n&&l<=n) {
ll ss=abss(p[r].sum-p[l].sum); if(ans>abss(ss-s)) {
anss=ss;
ans=abss(ss-s);
ansl=p[l].id;
ansr=p[r].id;
}
if(ss>s) {
l++;
} else if(ss<s) {
r++;
} else break;
if(l==r) {
r++;
}
}
if(ansl>ansr)swap(ansl,ansr);
cout<<anss<<" "<<ansl+1<<" "<<ansr<<endl;
}
}
return 0;
}

Bound Found(思维+尺取)的更多相关文章

  1. POJ 2566 Bound Found 尺取 难度:1

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1651   Accepted: 544   Spec ...

  2. B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值

    B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...

  3. poj2566尺取变形

    Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...

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

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

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

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

  6. 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 ...

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

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

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

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

  9. poj2100还是尺取

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

随机推荐

  1. Jquery中extend使用技巧

    在使用Jquery开发的过程中,extend是常用的参数处理函数,特别是对默认值的使用. Jquery的扩展方法原型是: var v=$.extend(dest,src1,src2,[,src3... ...

  2. Tensorflow学习(练习)—下载骨骼图像识别网络inception数据集

    import tensorflow as tfimport osimport tarfileimport requests #inception模型下载地址inception_pretrain_mod ...

  3. 720. Longest Word in Dictionary 能连续拼接出来的最长单词

    [抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...

  4. TOP命令 详解CPU 查看多个核心的利用率按1

    top命令是linux下常用的工具,可以查看各个进程的CPU使用情况.先看一个实例: 这是Ramnode双核VPS的top显示结果: 左上角可以看到CPU的使用率是11.3%,但是看下面的进程,plu ...

  5. Docker学习笔记_安装和使用Rabbitmq

    一.准备 1.宿主机OS:Win10 64bit 2.虚拟机OS:Ubuntu18.04 3.账号:docker 4.虚拟机IP:192.168.8.25 二.安装 1.搜索镜像            ...

  6. 一次shell中seq的处理

    一次shell中seq的处理 背景:用要shell 提取 文件中内容,文件名是用序列号如下生成,文件差不多有将近400多w个  如下:  www.ahlinux.com 原始脚本#! /bin/sh# ...

  7. 用TCGA数据库分析癌症和癌旁组织的表达差异

    上周收到一条求助信息:“如何用TCGA数据库分析LINC00152在卵巢癌与正常组织的的表达差异?” 所以以这个题目为记录分析过程如下: 一.下载数据 a)进入网站https://cancergeno ...

  8. C# 中使用using的三种方法

    1.using指令 using+命名空间,这种方法基本学习过C#的都用过,好处在于,写代码的时候不需要指定详细的命名空间 using System.Windows.Media; using Syste ...

  9. 没有为扩展名“.cshtml”注册的生成提供程序

    新建的mvc4 项目,然后从其他项目里拷贝shared文件夹和_ViewStart.cshtml文件过去,然后在@符号上出现“没有为扩展名“.cshtml”注册的生成提供程序” 解决方法: 需要在项目 ...

  10. 如何从Spring官网下载Spring的jar包

    Spring官网:https://spring.io/ 进入官网点击PRODECTS 然后点击Spring Framework 进入下面的页面点击小猫图标: 之后再下面的页面持续向下滚动,找到下图我标 ...