POJ 2566
| Time Limit: 5000MS | Memory Limit: 65536K | |||
| Total Submissions: 1445 | Accepted: 487 | 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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define maxn 100005
#define INF 2000000000 typedef pair<int,int> pii; int n,k;
pii a[maxn]; int Abs(int x) {
return x > ? x : -x;
} void solve(int x) {
int sum = ,s = ,pos = ,v,ans = INF,l,r;
//printf("ans = %d\n",ans);
for(; s <= n && pos <= n;) {
int tem = a[pos].first - a[s].first;
//printf("tem = %d\n",tem);
if( Abs(tem - x) < ans) {
ans = Abs(tem - x);
l = a[s].second;
r = a[pos].second;
v = tem;
} if(tem > x) {
++s;
} else if(tem < x) {
++pos;
} else {
break;
}
if(s == pos) ++pos; }
if(l > r) swap(l,r); printf("%d %d %d\n",v,l + ,r);
} int main() {
// freopen("sw.in","r",stdin); while(~scanf("%d%d",&n,&k) ) {
if(!n && !k) break;
int sum = ;
a[] = pii(,);
for(int i = ; i <= n; ++i) {
int ch;
scanf("%d",&ch);
sum += ch;
a[i] = make_pair(sum,i); } sort(a,a + n + ); for(int i = ; i <= k; ++i) {
int t;
scanf("%d",&t);
solve(t);
} }
return ;
}
POJ 2566的更多相关文章
- 尺取法 poj 2566
尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...
- B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值
B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...
- POJ 2566:Bound Found(Two pointers)
[题目链接] http://poj.org/problem?id=2566 [题目大意] 给出一个序列,求一个子段和,使得其绝对值最接近给出值, 输出这个区间的左右端点和区间和. [题解] 因为原序列 ...
- Greedy:Bound Found(POJ 2566)
神奇密码 题目大意:就是给你一个数组,要你找出连续的数的绝对值的和最接近t的那一串,并且要找出数组的上界和下界的下标,并显示他们的和 因为这一题的数有正有负,所以必须要先把和求出来,然后排序,然 ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- POJ 2566 尺取法(进阶题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4297 Accepted: 1351 Spe ...
- poj 2566 Bound Found
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4384 Accepted: 1377 Spe ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
随机推荐
- Swing基础
Swing基础 JFrame JPanel 绘图:paint 监听事件: ActionListener KeyListener Listener和Adapter 计时器:Timer Time ...
- C 实现一个跨平台的定时器 论述
引言 今天我们要讲述和构建的是一个跨平台多线程C的定时器对象,粒度是毫秒级别.可以称之为简易的timer, sctimer.h 库. 首先看总的接口,看门见客. sctimer.h #ifndef _ ...
- SQL基础篇---基本概念解析
1.数据库database:保存表和其他相关SQL结构容器(一般是一个文件或者一组文件) 2.SQL (Structared Query Language):是一种专门用来与数据库沟通的语言,是一种结 ...
- 配置php5.6的运行环境
所需要的原材料:(提供链接) php-5.6.10-Win32-VC11-x86 (zip)(注意php版本分为了IIS版和Apache版) httpd-2.4.12-x86-r2(apache) ( ...
- java 单例模式总结
单例模式的实现方式总结: 第一种方式:同步获取实例的方法,多线程安全,懒汉模式.在调用实例的时刻初始化. public class Singleton1 { private static Single ...
- ping通IP,telnet 3306不通
一个同事装的MySQL数据库,无法连接.1.查看权限2.查看防火墙检查用户权限,防火墙都没问题,就是无法连接,能ping通,但是telnet 3306 端口无法成功.检查了下数据库配置 ...
- oracle 11g 修改默认监听端口1521
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...
- iOS开发之构建Widget
原文出处: 陈凯 在 jianshu 的博客(@chenkaiHome) 欢迎分享原创到伯乐头条 伴随这iOS 8 系统多达4000项API更新而来同样还有Today Extension.而对iO ...
- OC中数组类NSArray的详解,数组的遍历(二)
数组类的便利 1.for循环(大家都会的...) 2.NSEmunerator 3.for in 首先重点说下 第二种NSEmunerator枚举器,系统声明是 @interface NSEnumer ...
- 微软职位内部推荐-Sr SDE
微软近期Open的职位: MSN reaches nearly half a billion people across the globe where we are the #1 portal in ...