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 ...
随机推荐
- rspec的一些常见用法
这里讲了如何安装rspec,安装使用rspec. 下面介绍一下rspec中常见的使用方法. 下面是一个最简单的测试用例,判断true是不是等于true,should_be是旧的用法,新用法推荐使用ex ...
- 多线程基本概论multithread
多线程 基本概念 进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 通过 活动监视器 可以查看 Mac 系统中所开启的进程 线程 进程要想 ...
- C 基于UDP实现一个简易的聊天室
引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...
- 10 款提高开发效率的 jQuery/CSS3 组件
前端开发是一项十分繁琐而又耗体力的工作,如何更有效率的开发我们的应用,很多人会选择适当地使用一些jQuery插件.今天就要给大家分享10款可以提高开发效率的jQuery/CSS3组件.部分插件可以下载 ...
- ORA-01078、ORA-01565、ORA-17503、ORA-29701
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - ...
- memcached 高级机制(二)
memcached删除机制 a) (1)有内存机制里说明了,这里会运用到LRU删除机制.我们知道,当我们在add或set一个值时,我们会设置这个值得期限.当某个值过期后,这个值并没有从内存中删除,我们 ...
- Redbean:入门(一) - 增删改查
<?php require_once 'rb.php'; $tableName = "link"; //链接数据库 R::setup("mysql:host=loc ...
- Android greenDao的简单配置和使用
最近自学做东西的时候用到了一个收藏的功能,然后我想把东西存放到SQLite当中,然而自己传值的时候都是用到的实体类,所以存起来也比较麻烦,所以从网上找到一个greenDao的开源框架非常火,不仅效率高 ...
- hdu 1269 迷宫城堡
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个 ...
- Objective-C-实例变量与属性的关系
当在一个类创建一个属性,Xcode编译器就会自动产生一个带下划线的同名实例变量: 一般来说,如果getter这个属性采用下划线的方式获取效率更高,而setter采用self.属性名更加合理. 读取实例 ...