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 ...
随机推荐
- PAT 1087 有多少不同的值(20)(STL—set)
1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...
- MacDev.GetArchOfLibrary
1. static library How to check target architecture of a static library http://changhoward.blogspot.c ...
- CF Round #510 (Div. 2)
前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...
- Git 分支 主干
~/Desktop/work/movies/movie(apps) $ git status //先查看是否有需要提交的东西# On branch appsnothing to commit (wo ...
- Vim中如何使用正则进行搜索
#set magic这句的作用在于将vim的正则打开. 这样就可以搜索了. 至于正则的相关的内容.在此抄一份.以免于下次自己还要去百度. 符号 匹配 . (dot) 任意单一字符 \d 任意一位数字 ...
- 20岁的设计师vs30岁的设计师
20岁的设计师vs30岁的设计师 如果你还是20来岁,要恭喜你,你还年轻, 一切才刚刚开始 还有时间去探索无尽的可能 还有时间去找到无限的前途 如果30岁的你还不够强大, 请记得时刻给予自己信心, ...
- UI设计:C4D作品案例分享
中文名4D电影,外文名CINEMA 4D,研发公司为德国Maxon Computer,特点为极高的运算速度和强大的渲染插件,使用在电影<毁灭战士>.<阿凡达>中,获得贸易展中最 ...
- [Hbase]Hbase章1 Hbase框架及基本概念
Hbase框架介绍 HBase是一个分布式的.面向列的开源数据库. 不同点: l 和一般的关系数据库不同,hbase是一个适合于非结构化数据存储的数据库. l Hbase是基于列而不是基于行的模式 ...
- Maximum Average Subarray I LT643
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- so文件相关
2018-08-31 今天尝试了一下编译so文件. 最开始是按照这个博主来操作的https://blog.csdn.net/tianshuai4317618/article/details/79073 ...