Bound Found

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 5408 Accepted: 1735 Special Judge

Description

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


解题心得:

  1. 给你一些列数,你选择一段连续的区间,区间总和的绝对值最接近给定的数,但是这些数可能是负数。
  2. 这个题主要的难点就是这些数可能是一个负数,有负数那么区间越长并不代表总和越大,这里就需要明确尺取法的两个特点
    • 第一个特点就是尺取必须是选择连续的一段区间。
    • 第二就是随着尺取长度的增加要有一个单调性,递增或者递减。
  3. 这个题明显不符合第二个特点随着区间长度的增加没有一个单调性,这个时候按照传统的尺取就没办法了。但是题目上并没有对于区间长度有什么要求,仅仅是要求总和绝对值和给定数相差最小,要求的是区间和,区间和可以从前缀和得到。这样就可以先得到前缀和,然后对于前缀和排序,这样就有了单调性,然后对于递增的前缀和用尺取法得最接近的给定数的区间。

#include <algorithm>
#include <stdio.h>
#include <climits> using namespace std;
const int maxn = 1e5 + 100; int t, n, m;
pair<int, int> p[maxn]; int main() {
while (scanf("%d%d", &n, &m) && n | m) {
p[0] = make_pair(0, 0);//要多插入一个为零的总和
for (int i = 1; i <= n; i++) {
int temp;
scanf("%d", &temp);
p[i] = make_pair(p[i - 1].first + temp, i);
}
sort(p, p + n + 1); while (m--) {
scanf("%d", &t);
int l, r, sum = INT_MAX, ans_l, ans_r, ans = INT_MAX;
l = 0;r = 1;
while(r <= n) {
int temp = p[r].first - p[l].first;
if(abs(temp - t) < ans) {
ans = abs(temp-t);
sum = temp;
ans_l = min(p[l].second,p[r].second);
ans_r = max(p[l].second,p[r].second);
}
if(temp < t) r++ ;
else l++;
if(l == r)
r++;
} printf("%d %d %d\n", sum, ans_l + 1, ans_r);
}
}
return 0;
}

最新文章

  1. Sql Server之旅——第九站 看公司这些DBA们设计的这些复合索引
  2. flex的http URL转码与解码
  3. winform实现word转换为PDF(.doc)
  4. python判断文件目录是否存在
  5. 软件测试技术(三)——使用因果图法进行的UI测试
  6. mongodb 教程三
  7. 高性能MySql学习笔记——锁、事务、隔离级别(转)
  8. System.ComponentModel.BackgroundWorker在WinForm中的异步使用
  9. MySQL集群PXC的搭建
  10. Web开发技术的演变
  11. 重装助手教你如何禁用Windows 10快速启动
  12. 【webpack学习笔记】a04-建立开发环境
  13. Linux:Day24(下) samba
  14. IOT相关协议
  15. SpringBoot事务注解@Transactional
  16. vue基础——Class与Style绑定
  17. 数据透视表sql:用SQL行列转换实现数据透视的一些思考
  18. Django之图书管理系统
  19. Problem D: 程序填充(递归函数):数列2项和
  20. 自己搭建v,p,n过程

热门文章

  1. Python常用模块(一)
  2. Azure 6 月新公布
  3. Office加载项
  4. Hadoop federation配置
  5. 随便讲讲自己了解的ajax在JQ中的应用
  6. graphql 项目搭建(二)
  7. Oracle编程入门经典 第11章 过程、函数和程序包
  8. MySQL入门很简单: 10 mysql运算符
  9. 使用selenium grid与BrowserMobProxyServer联合使用
  10. C++之string基本字符系列容器