Bound Found(思维+尺取)
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two parts: a sequence 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
The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n=k=0. Otherwise, 1<=n<=100000 and there follow n integers with absolute values <=10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0<=t<=1000000000.
Output
For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.
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
思路:这个题拿到手,很容易想到的是暴力,但是很明显,必然会超时,所以,我们要换种思路,既然是尺取题,尺取关键要单调,这是个比较难处理的过程,没想好如何去尺取处理,然后参考了一篇博客,恍然大悟,博客链接:https://blog.csdn.net/acm_cxq/article/details/51854210,很巧妙的做法,只是没想到,关键就是求和排序,这是个突破点
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define INF 0x3f3f3f3f
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll a[maxn];
ll abss(ll x) {
if(x<0) {
return -x;
} else {
return x;
}
}
struct node {
ll sum,id;
} p[maxn];
bool cmp(node x,node y) {
return x.sum<y.sum;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,k;
while(cin>>n>>k) {
if(n==0&&k==0) {
break;
}
p[0].sum=0,p[0].id=0;
for(int t=1; t<=n; t++) {
cin>>a[t];
p[t].sum=p[t-1].sum+a[t];
p[t].id=t;
}
sort(p,p+n+1,cmp);
int s;
for(int t=0; t<k; t++) {
scanf("%d",&s);
ll l=0,r=1;
ll ans=INF;
ll ansl,ansr,anss;
while(r<=n&&l<=n) {
ll ss=abss(p[r].sum-p[l].sum);
if(ans>abss(ss-s)) {
anss=ss;
ans=abss(ss-s);
ansl=p[l].id;
ansr=p[r].id;
}
if(ss>s) {
l++;
} else if(ss<s) {
r++;
} else break;
if(l==r) {
r++;
}
}
if(ansl>ansr)swap(ansl,ansr);
cout<<anss<<" "<<ansl+1<<" "<<ansr<<endl;
}
}
return 0;
}
Bound Found(思维+尺取)的更多相关文章
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
- B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值
B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- POJ:2566-Bound Found(尺取变形好题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...
- Gym 100703I---Endeavor for perfection(尺取)
题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)
题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...
- poj2100还是尺取
King George has recently decided that he would like to have a new design for the royal graveyard. Th ...
随机推荐
- 基于PCL绘制模型并渲染
博客转载自:https://blog.csdn.net/wokaowokaowokao12345/article/details/51321988 前言 抛开算法层面不谈,要利用PCL库中PCLVis ...
- boost 时间与日期处理
博客转载自: 类 特点 缺点 说明 timer 计时基类 不适合大跨度时间 适用大部分的普通计时 progress_timer 继承自timer 可以自动写入流中 只精确到0.01s 如果需要更精确, ...
- java方法学习记录
---恢复内容开始--- 方法重载:两个方法有相同的名字,但参数不同,就是方法重载,且不能仅仅依据修饰符或者返回类型的不同来重载方法. 命令行参数的使用 有时候你希望运行一个程序时候再传递给它消息.这 ...
- Luogu 3646 [APIO2015]巴厘岛的雕塑
初赛成绩出了,和预想的一样,一分都没挂,开心. 大佬的博客 subtask 1 ($n \leq 200$) 因为高位取$0$一定比高位取$1$优,我们考虑按照位从高到低进行检验,设$f_{i, j} ...
- 框架和cms区别
cms:精装房,拎包即可入住 框架:毛坯房,内部装修可根据自己需求来 选择策略:个性化需求不高的话,可以选择cms
- Office Web APP预览如何去掉顶部版权标志“Microsoft Office Web Apps”
在Office Web APP的预览会涉及4中类型的文 件:Word.Excel.PowerPoint.PDF,不同的类型在预览时调用的文件是不一样的,其中Word和 PDF调用的是同一个文件.每个预 ...
- css总结16:HTML5 多媒体音频(Audio)视频(video )
1 显示嵌入网页中的 MP3 文件: <embed height="50" width="100" src="horse.mp3"&g ...
- TinyMCE4.x整合教程-Xproer.WordPaster
版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...
- ASP.NET中Service接受前端单个及多个数据的写法
MVC中的Service主要是用来处理数据交互的. 前端页面传递一个或者多个参数到Service的写法: 一个参数写法: public DataTable 方法名 (Pagination pagina ...
- Android 中menu在activity中的使用
1.在res下选择new 选择Android resource directory 2.在弹出框中Resource type选择menu 3.在res下就可以看到menu文件夹 4.在menu文件夹 ...