poj 2566 Bound Found 尺取法 变形
| Time Limit: 5000MS | Memory Limit: 65536K | |||
| Total Submissions: 2277 | Accepted: 703 | 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<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
pair<int, int > p[100005];
int n, m, k;
void solve(int k)
{
int l = 0, r = 1, al, ar, av, minn = inf;
while (l<=n&&r<=n&&minn!=0)
{
int temp=p[r].first - p[l].first;
if (abs(temp - k) < minn)
{
minn = abs(temp - k);
ar = p[r].second;
al = p[l].second;
av = temp;
}
if (temp> k)
l++;
else if (temp < k)
r++;
else
break;
if (r == l)
r++;
}
if(al>ar)
swap(al,ar);//因为al和ar大小没有必然关系()取绝对值,所以//要交换
printf("%d %d %d\n", av, al+1, ar);
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
if (!n&&!m) return 0;
p[0] = make_pair(0, 0);
for (int i = 1; i <= n; i++)
{
scanf("%d", &p[i].first);
p[i].first += p[i - 1].first;
p[i].second = i;
}
sort(p, p + n + 1);
while (m--)
{
scanf("%d", &k);
solve(k);
}
}
return 0;
}
下面是自己的wa代码
好好找茬
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
pair<int, int > p[100005];
int n, m, k;
void solve(int k)
{
int l = 0, r = 1, al, ar, av, minn = inf;
while (l<=n&&r<=n)
{
int temp = p[r].first - p[l].first;
if (abs(temp - k) < minn)
{
minn = abs(temp - k);
ar = p[r].second;
al = p[l].second;
av = temp;
}
if (temp > k)
l++;
else if (temp < k)
r++;
else
break;
if (r == l)
r++;
}
printf("%d %d %d\n", av, al+1, ar);
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
if (!n&&!m) return 0;
p[0] = make_pair(0, 0);
for (int i = 1; i <= n; i++)
{
scanf("%d", &p[i].first);
p[i].first += p[i - 1].first;
p[i].second = i;
}
sort(p + 1, p + n + 1);
while (m--)
{
scanf("%d", &k);
solve(k);
}
}
return 0;
}
poj 2566 Bound Found 尺取法 变形的更多相关文章
- POJ 2566 Bound Found(尺取法,前缀和)
		
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
 - poj 2566"Bound Found"(尺取法)
		
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
 - poj 2566 Bound Found 尺取法
		
一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...
 - POJ 2566 Bound Found 尺取 难度:1
		
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
 - poj 2566 Bound Found(尺取法 好题)
		
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
 - POJ:2566-Bound Found(尺取变形好题)
		
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...
 - poj 3061(二分 or 尺取法)
		
传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...
 - POJ  3061    Subsequence    ( 尺取法)
		
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...
 - poj 3320 复习一下尺取法
		
尺取法(two point)的思想不难,简单来说就是以下三步: 1.对r point在满足题意的情况下不断向右延伸 2.对l point前移一步 3. 回到1 two point 对连续区间的问题求 ...
 
随机推荐
- windows 装mac
			
必备条件: 1.vmware虚拟机 2.给相应版本虚拟机打mac补丁 3.用securable检测CPU支持虚拟化设置 4.mac镜像文件 5.这时候还不能启动虚拟机,还需要在引导文件里面进行参数修改 ...
 - 医院医疗类报表免费用,提反馈,还能赢取P30!
			
医院医疗类报表免费用,提反馈,还能赢取P30! “葡萄城报表模板库是一款免费的报表制作.学习和参考工具,包含了超过 200 张高质量报表模板,涵盖了 16 大行业和 50 多种报表类型,为 30 余万 ...
 - Linux中使用curl命令发送带参数的get请求和post请求
			
GET 请求 curl命令 + 请求接口的地址 curl http://**.**.***.**/SeedAgile/SeedApi/querySprintByRequirementNo?parame ...
 - Docker 添加容器SSH服务
			
很多时候我们需要登陆到容器内部操作,此时我们就需要开启容器的SSH支持了,下面的小例子将具体介绍三种分配IP地址的方法,分别是pipworl分配,commit分配,Docker分配等. 基于commi ...
 - poj2226-Muddy Fields二分匹配 最小顶点覆盖 好题
			
题目 给到一个矩阵,有些格子上是草,有些是水.需要用宽度为1,长度任意的若干块木板覆盖所有的水,并不能覆盖草,木板可以交叉,但只能横竖放置,问最少要多少块板. 分析 经典的矩阵二分图构图和最小点覆盖. ...
 - 这38个小技巧告诉你如何快速学习MySQL数据库2
			
1.如何快速掌握MySQL? ⑴培养兴趣兴趣是最好的老师,不论学习什么知识,兴趣都可以极大地提高学习效率.当然学习MySQL 5.6也不例外.⑵夯实基础计算机领域的技术非常强调基础,刚开始学习可能还认 ...
 - IE浏览器清除缓存及历史浏览数据
			
IE浏览器清除缓存方法如下: 打开IE浏览器,依次点击"工具-Internet选项-常规-删除",如下图所示, 有的时候发现你明明已经执行了删除,但是实际上还是有缓存数据,一般是因 ...
 - 安装vim自动补全插件
			
1 安装VIM 2 安装vim插件管理工具.过程见链接.(谢谢) 3 在.vimrc中添加下列代码 Bundle 'Valloric/YouCompleteMe' 保存退出后打开vim,在正常模式下输 ...
 - 一次NaN引发的npe
			
# 先上代码 import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class QQ { p ...
 - 牛客练习赛47 E	DongDong数颜色 (树状数组维护区间元素种类数)
			
链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...