POJ2566 Bound Found 2017-05-25 20:05 32人阅读 评论(0) 收藏
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 4056 | Accepted: 1249 | Special Judge |
Description
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
this sequence. Each query is a target t with 0<=t<=1000000000.
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 <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
#define LL long long
const int inf=0x7fffffff;
int n,m,k;
int a[100005];
struct node
{
int id,val;
} pre[100005];
bool cmp(node a,node b)
{
return a.val<b.val;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
pre[0].val=0;
pre[0].id=0;
for(int i=1; i<=n; i++)
pre[i].val=pre[i-1].val+a[i],pre[i].id=i; sort(pre,pre+n+1,cmp); for(int i=0; i<m; i++)
{
scanf("%d",&k);
int ans=inf;
int ansl,ansr;
int l=0,r=1;
int sum;
while(l<=n&&r<=n)
{
sum=pre[r].val-pre[l].val;
if(abs(sum-k)<abs(ans-k))
{
ans=sum;
ansl=min(pre[l].id,pre[r].id)+1;
ansr=max(pre[l].id,pre[r].id);
}
if(sum<k)
r++;
else if(sum>k)
l++;
else
break;
if(l==r)
r++;
}
printf("%d %d %d\n",ans,ansl,ansr);
} }
return 0;
}
POJ2566 Bound Found 2017-05-25 20:05 32人阅读 评论(0) 收藏的更多相关文章
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- 高质量C++C编程指南笔记 标签: c++笔记 2015-11-22 20:59 179人阅读 评论(0) 收藏
1. 在多重循环中,如果有可能,应当将最长的循环放在最内层,最短的循环放在最外层,以减少 CPU 跨切循环层的次数. 2. 如果循环体内存在逻辑判断,并且循环次数很大,宜将逻辑判断移到循环体的外面 ...
- Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏
Self Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22101 Accepted: 12429 De ...
- Codeforces812B Sagheer, the Hausmeister 2017-06-02 20:47 85人阅读 评论(0) 收藏
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...
- UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏
UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...
- OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏
1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...
- ZOJ2748 Free Kick 2017-04-18 20:40 40人阅读 评论(0) 收藏
Free Kick Time Limit: 2 Seconds Memory Limit: 65536 KB In a soccer game, a direct free kick is ...
- NYOJ 119 士兵杀敌(三)【ST算法】 分类: Brush Mode 2014-11-13 20:56 101人阅读 评论(0) 收藏
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=119 解题思路: RMQ算法. 不会的可以去看看我总结的RMQ算法. http://blo ...
- Add listitem with javascript 分类: Sharepoint 2015-07-16 20:23 4人阅读 评论(0) 收藏
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);//makes sure sp.js is loaded and the ...
随机推荐
- Hibbernate详解一
这里先做一个简单的入门,后面有详解 记住图解原理: 这里只是没有整合spring等项目使用的hibernate的使用详解. 一.Hibernate简介 1.Hibernate在开发中所处的位置 2.O ...
- 改变dos的编码方式
chcp 936 改变成 gbk chcp 65001 改成 utf-8 删除MySqlite文件 generic_x86:/data/data/com.example.lifen.sqlite/da ...
- 八皇后问题(dfs)
#include <iostream> #include <stdio.h> using namespace std; ; ], b[], c[], vis[][]; //a, ...
- iOS.BackgroundTask
Background Task 的运行时间在iOS 6及以前有大约10分钟左右,在iOS 7中有180秒. Reference: 1. Multitasking in iOS 7 http://www ...
- BZOJ4033或洛谷3177 [HAOI2015]树上染色
BZOJ原题链接 洛谷原题链接 很明显的树形\(DP\). 因为记录每个点的贡献很难,所以我们可以统计每条边的贡献. 对于每一条边,设边一侧的黑点有\(B_x\)个,白点有\(W_x\),另一侧黑点有 ...
- PHP编程时的规范化命名
要想成为一名“合格”的程序员,就必须要有良好的编程习惯和规范,这样做的好处有很多,诸如:可以提高代码质量,提高程序的可维护性,提高开发速度和效率等.以下就简要的列出几条日常编写程序时大概要注意的一些“ ...
- php mysql 丢失更新
php mysql 丢失更新问题,搜索整个互联网,很少有讲到,也许和php程序员出身一般都是非科班出身有关系吧. 另外php程序一般都是简单数据,很少有并发一致性问题,所以大家都没有谁专门提出这个问题 ...
- PS快捷键大全,记住这些就够了!
希望能帮到大家更好的学习.
- idea15 生成mybatis代码
pom.xml <build> <finalName>mybatis_generator</finalName> <plugins> <plugi ...
- rbtposeekf的注意事项
1.发布的odom topic以及 imu topic必须加上协方差部分:2.在发布odom的时候,去掉里面的odom->base_link的tf,因为这个tf会在robot_pose_ekf包 ...