POJ:2566-Bound Found(尺取变形好题)
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
解题心得:
- 给你一些列数,你选择一段连续的区间,区间总和的绝对值最接近给定的数,但是这些数可能是负数。
- 这个题主要的难点就是这些数可能是一个负数,有负数那么区间越长并不代表总和越大,这里就需要明确尺取法的两个特点
- 第一个特点就是尺取必须是选择连续的一段区间。
- 第二就是随着尺取长度的增加要有一个单调性,递增或者递减。
- 这个题明显不符合第二个特点随着区间长度的增加没有一个单调性,这个时候按照传统的尺取就没办法了。但是题目上并没有对于区间长度有什么要求,仅仅是要求总和绝对值和给定数相差最小,要求的是区间和,区间和可以从前缀和得到。这样就可以先得到前缀和,然后对于前缀和排序,这样就有了单调性,然后对于递增的前缀和用尺取法得最接近的给定数的区间。
#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;
}
POJ:2566-Bound Found(尺取变形好题)的更多相关文章
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
- poj 2566 Bound Found 尺取法 变形
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2277 Accepted: 703 Spec ...
- poj 2566 Bound Found 尺取法
一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- POJ 2566 Bound Found(尺取法,前缀和)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- poj 2566 Bound Found
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4384 Accepted: 1377 Spe ...
- Subsequence (POJ - 3061)(尺取思想)
Problem A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
随机推荐
- (生产)axios - 请求接口
参考:https://www.awesomes.cn/repo/mzabriskie/axios axios 介绍 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中 ...
- IIS 发表web 之后,访问注册表项失败得问题
错误: 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\xx\xxxx\xxxxx”的访问被拒绝. 解决办法: 打开IIS,找到应用程序池,然后找到自己web使用得程序池,右键高级 ...
- JavaScript(一) 对象基础
1.定义类或对象 1.1 混合的构造函数/原型方法 用构造函数定义对象的所有非函数属性,类似于Java的构造方法.用原型方法定义对象的函数属性(方法).这种方法是使用比较广泛的定义类或对象的方法. / ...
- 01、Spark安装与配置
01.Spark安装与配置 1.hadoop回顾 Hadoop是分布式计算引擎,含有四大模块,common.hdfs.mapreduce和yarn. 2.并发和并行 并发通常指针对单个节点的应对多个请 ...
- 快速提取邮箱地址(利用word或网站)
在word中,CTRL+F,输入:[A-z,0-9]{1,}\@[A-z,0-9,\.]{1,} 点击“高级”,勾选“使用通配符”,点击“查找全部”: 复制.粘贴. 还可通过以下页面在线提取. htt ...
- IOS 读取xib到tabelView
/**每一行显示怎样的ceLl*/ -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIn ...
- Linux MySQL 5.1源码安装
安装必备的软件 yum install ncurses-devel -y yum install pcre pcre-devel -y yum install gcc* -y 解压缩 tar -z ...
- mongodb在C#的连接以及curd写法
连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...
- 线程属性总结 线程的api属性
http://blog.csdn.net/zsf8701/article/details/7842392 //线程属性结构如下:typedef struct{ int etachstate; //线程 ...
- 【BZOJ4555】[TJOI2016&HEOI2016] 求和(NTT)
点此看题面 大致题意: 计算\(\sum_{i=0}^n\sum_{j=0}^iS(i,j)*2^j*(j!)\),其中\(S\)为第二类斯特林数. 推式子 首先让我们来推一波式子: 因为当\(i&l ...