题意:
输入 n m  之后输入n个数 
之后m个询问  对于每个询问 输入一个t    输出  三个数 ans l r  表示从l 到 r的所有数的和的绝对值最接近t 且输出这个和ans
 
思路:就是指针的移动。
 
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<limits.h>
using namespace std; pair<int,int>p[];
int n,m,t,num; void find(int x)
{
int i=,j=,ans=INT_MAX,temp,l,r,v;
while(j<=n && ans)
{
temp=p[j].first-p[i].first;
if(abs(temp-x)<=ans)
{
ans=abs(temp-x);
v=temp;
l=p[i].second;
r=p[j].second;
}
if(temp<x) j++;
if(temp>x) i++;
if(i==j) j++;
}
if(l>r)
{
int a=r;
r=l;
l=a;
}
printf("%d %d %d\n",v,l+,r);
} int main()
{
while(scanf("%d%d",&n,&m)== && (m+n))
{
int sum=;
p[]=make_pair(,);
for(int i=; i<=n; i++)
{
scanf("%d",&num);
sum+=num;
p[i]=make_pair(sum,i);
}
sort(p,p+n+);
while(m--)
{
scanf("%d",&t);
find(t);
}
}
return ;
}

poj2566 尺取法的更多相关文章

  1. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

  2. Bound Found [POJ2566] [尺取法]

    题意 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. Input The input file contains several test cases. Each t ...

  3. poj 2566"Bound Found"(尺取法)

    传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...

  4. POJ 尺取法

    poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...

  5. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  6. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  8. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  9. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

随机推荐

  1. Enum:EXTENDED LIGHTS OUT(POJ 1222)

    亮灯 题目大意:有一个5*6的灯组,按一盏灯会让其他上下左右4栈和他自己灯变为原来相反的状态,要怎么按才会把所有的灯都按灭? 3279翻版题目,不多说,另外这一题还可以用其他方法,比如DFS,BFS, ...

  2. mybatis延迟加载

    配置完成后可能会报错Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath 是由于 ...

  3. ios调用c#后台接口报文格式

    - NSString *soapMessage = - [NSString stringWithFormat: - @"<?xml version=\"1.0\" ...

  4. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  5. 10.11 cocoapods安装

    手动安装gem 手动下载 rubygem https://rubygems.org/pages/download#formats 10.11 cocoapods安装 sudo gem install ...

  6. iOS自动更新如何实现

    APP检测更新可以使用两种方法.第一种是和安卓等系统一样,获取自己服务器的APP版本号与已安装的APP版本号比较:第二种是根据已发布到app store上的应用版本号与已安装的APP版本号比较更新.第 ...

  7. mysql中select五种子句和统计函数

    select 五种子句顺序 where 条件 group by 分组 having 把结果进行再次筛选 order by  排序 limit  取出条目 统计函数  max(列名)  求最大 min( ...

  8. 数据结构和算法 – 7.散列和 Hashtable 类

    7.1.散列函数 散列是一种常见的存储数据的技术,按照这种方式可以非常迅速地插入和取回数据.散列所采用的数据结构被称为是散列表.尽管散列表提供了快速地插入.删除.以及取回数据的操作,但是诸如查找最大值 ...

  9. android 屏幕旋转

    转自:http://blog.csdn.net/oyzhizhong/article/details/8131799 屏是LANDSCAPE的,要让它默认显示为PORTRAIT. 1.kernel里要 ...

  10. ora-01658 :无法为表空间USERS 中的段创建INITIAL区

    "CREATE INDEX "IDX_TS_BONUS_Q_201209_DS" ON "TS_BONUS_Q_201209" ("DS&q ...