POJ 2566 尺取法(进阶题)
| Time Limit: 5000MS | Memory Limit: 65536K | |||
| Total Submissions: 4297 | Accepted: 1351 | Special Judge | ||
Description
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
Output
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
Source
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define db double
#define inf 0x3f3f3f
#define mj
//#define ll long long
#define unsigned long long ull;
using namespace std;
const int mod = ;
const int N=1e6+;
const double eps = 1e-;
typedef pair<int, int > pii;
pii p[N];
int n, m, k;
void f(int k)
{
int l = , r = , ll, rr, v, mi = inf;
while (l<=n&&r<=n&&mi!=)
{
int tmp=p[r].first - p[l].first;
if (abs(tmp - k) < mi)
{
mi = abs(tmp - k);
rr = p[r].second;
ll = p[l].second;
v = tmp;
}
if (tmp> k)
l++;
else if (tmp < k)
r++;
else
break;
if (r == l)
r++;
}
if(ll>rr) swap(ll,rr);//因为ll和rr大小没有必然关系()取绝对值,所以//要交换
printf("%d %d %d\n", v, ll+, rr);
}
int main()
{
while (scanf("%d %d", &n, &m)==,n||m)
{
p[]=make_pair(, );
for (int i = ; i <= n; i++)
{
scanf("%d", &p[i].first);
p[i].first += p[i - ].first;
p[i].second = i;
}
sort(p, p + n + );
while (m--)
{
scanf("%d", &k);
f(k);
}
}
return ;
}
POJ 2566 尺取法(进阶题)的更多相关文章
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值
B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- hdu 5056(尺取法思路题)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【尺取法好题】POJ2566-Bound Found
[题目大意] 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. [思路] ……前缀和比较神奇的想法.一般来说,我们必须要保证数列单调性,才能使用尺取法. 预处理出前i个 ...
- poj 2100(尺取法)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 6107 Accepted: 1444 ...
- POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...
- poj 1184 广搜进阶题
起初的想法果然就是一个6000000的状态的表示. 但是后面觉得还是太过于幼稚了. 可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小. #include<iostream> # ...
随机推荐
- Buffer深入分析
Buffer Buffer的类图如下: 除了Boolean,其他基本数据类型都有对应的Buffer,但是只有ByteBuffer才能和Channel交互.只有ByteBuffer才能产生Direct的 ...
- xfire调用webservice接口的实现方式
package com.test; import java.net.URL; import org.codehaus.xfire.client.Client; import org.codehaus. ...
- ionic中应用sass
在学习ionic过程中看到sass,总结了一下基本用法和问题解决办法1.首先需要一个ionic项目,并执行下面的命令ionic start CustomSass blank && cd ...
- Jmeter-BeanShell PostProcessor提取请求及响应结果并保存到本地文件
1.新建一个本地csv文件,存放请求需要使用的变量值account,password,并配置CSV Data Set Config 2.添加一个HTTP请求 3.添加正则提取器用来提取响应结果中的re ...
- java 获得当前时间 年月日时分秒 星期几
<%SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");//设置日期格式SimpleDat ...
- 大数据 Hadoop,Spark和Storm
大数据(Big Data) 大数据,官方定义是指那些数据量特别大.数据类别特别复杂的数据集,这种数据集无法用传统的数据库进行存储,管理和处理.大数据的主要特点为数据量大(Volume),数据类别复 ...
- eclipse从SVN检出项目之后,项目出错
今天公司把我分配到另一个项目组工作,然后下午使用SVN检出项目,出了问题 1.从SVN检出项目之后,要导入jar包.结果右键项目找不到Build Path,问了大牛才知道是这里的问题,一共四个步骤解决 ...
- OpenShitf 使用教程
OpenShitf https://www.openshift.com 1.注册openshitf账号 https://www.openshift.com/app/account/new 2.gett ...
- 想从事IT行业的你,一定看看这篇文章
很多想从事IT行业的小伙伴都会问: 我该如何学习技术? 我应该选择什么样的方向来深入学习并以此来就业? 如何证明自己的技术很牛? 什么是程序员的核心竞争力? 如何成为一名优秀的工程师? 对于这些疑问, ...
- 使用matplotlib绘制多轴图
一个绘图对象(figure)可以包含多个轴(axis),在Matplotlib中用轴表示一个绘图区域,可以将其理解为子图.上面的第一个例子中,绘图对象只包括一个轴,因此只显示了一个轴(子图).我们可以 ...