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 ...
随机推荐
- String类详解,StringBuffer
先说一下String类的equals()方法. 下面我们先看一段代码: 这段代码输出的结果为: ture true -------------- false 咋看之下貌似Object类比较特别,那么我 ...
- OC编写使用调试器
OC编写使用调试器 编写代码免不了,Bug.那么Debug就是程序员的必备技能了.本文和大家一起探讨,如何在应用开发编写代码过程中,使用日志项消息:以及使用动作.条件.迭代控制增强断点. 记录信息 在 ...
- GNU make 规则
clean : rm *.tmp 规则格式: targets : prerequisites recipe ... targets : prerequisites : recipe recipe .. ...
- MongoDB分片简单实例
分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据也足以提供可接受的读写吞吐量.这时,我 ...
- Tutorial: Facebook analytics using Power BI Desktop
In this tutorial you learn how to import and visualize data from Facebook. During the tutorial you'l ...
- supplicant
概述 wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep.wpa.wpa2等完整的加密认证,而iwconfig只能 ...
- Tornado服务器的学习
Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻塞 ...
- iOS 进阶 第二十天(0520)
0520 -KVO 如下图所示:(面试可能会问到,你就按照下面的说) 注意:NSString类型的成员变量用set方法时,要记得用copy,至于为什么,知道这么用就行了.如下图:
- 内部类&匿名内部类
内部类:如果A类需要直接访问B类中的成员,而B类又需要建立A类的对象.这时,为了方便设计和访问,直接将A类定义在B类中.就可以了.A类就称为内部类.内部类可以直接访问外部类中的成员.而外部类想要访问内 ...
- 修改Input中Placeholder默认提示颜色(兼容)
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #f00; } input:-moz-pl ...