【题目大意】

给出一个整数列,求一段子序列之和最接近所给出的t。输出该段子序列之和及左右端点。

【思路】

……前缀和比较神奇的想法。一般来说,我们必须要保证数列单调性,才能使用尺取法。

预处理出前i个数的前缀和,和编号i一起放入pair中,然而根据前缀和大小进行排序。由于abs(sum[i]-sum[j])=abs(sum[j]-sum[i]),可以忽视数列前缀和的前后关系。此时,sum[r]-sum[l]有单调性。

因此我们可以先比较当前sum[r]-sum[l]与t的差,并更新答案。

如果当前sum[r]-sum[l]<t,说明和还可以更大,r++。

同理,如果sum[r]-sum[l]>t,说明和还可以更小,l++。

如果sum[r]-sum[l]=t,必定是最小答案。

【注意点】

由于序列不能为空,即l<>r,如果l=r则r++。

我们更新答案的时候左右区间端点为乱序,输出的时候调整一下。

就OK了!

*本来想要尺取弄一个合集,然而这道题做的时候想了半天。还是单独拿出来吧orz

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=1e5+;
const int INF=;
pair<int,int> sum[MAXN];
int n,k,t; void init()
{
sum[]=make_pair(,);
int tmp=;
for (int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
tmp+=x;
sum[i]=make_pair(tmp,i);
}
sort(sum,sum+n+);
} void solve()
{
scanf("%d",&t);
int l=,r=,minans=INF,ans,ansl,ansr;
while (r<=n && minans)//这里一开始写成了ans,以后变量名不要取那么相像orz
{
int delta=sum[r].first-sum[l].first;
if (abs(delta-t)<=minans)
{
minans=abs(delta-t);
ans=delta;
ansl=sum[l].second;
ansr=sum[r].second;
}
if (delta<t) r++;
if (delta>t) l++;
if (l==r) r++;//☆注意序列不能为空!
}
if (ansl>ansr) swap(ansl,ansr);//注意排序后是无序的,左右区间要调整回有序
printf("%d %d %d\n",ans,ansl+,ansr);
} int main()
{
while (scanf("%d%d",&n,&k)!=EOF)
{
init();
for (int i=;i<=k;i++) solve();
}
return ;
}

【尺取法好题】POJ2566-Bound Found的更多相关文章

  1. poj 2566 Bound Found(尺取法 好题)

    Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...

  2. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  3. hdu 5056(尺取法思路题)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. Codeforces Round #364 (Div. 2) C. They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. codeforces #364c They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

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

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

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

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

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

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

  9. POJ 2566 Bound Found(尺取法,前缀和)

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5207   Accepted: 1667   Spe ...

随机推荐

  1. 当月第一天、最后一天、下月第一天,时间date

    时间记录,不是时间戳 $thismonth = date('m'); $thisyear = date('Y'); $startDay = $thisyear . '-' . $thismonth . ...

  2. PHP脚本运行时间

    http://www.cnblogs.com/zqifa/p/php-16.html PHP设置脚本最大执行时间的三种方法 php.ini 中缺省的最长执行时间是 30 秒,这是由 php.ini 中 ...

  3. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  4. Xcode下 gdb 调试命令

    Xcode的调试器为用户提供了一个GDB的图形化界面,GDB是GNU组织的开放源代码调试器.您可以在Xcode的图形界面里做任何事情:但是,如果您需要您可以在命令行里使用GDB的命令,且gdb可以在终 ...

  5. Mysql储存过程7: case

    #用在储存过程中: create procedure k() begin declare number int; )); case number then select '>0'; else s ...

  6. centos7安装lamp

    一.准备工作 1.   下载并安装CentOS7.2,配置好网络环境,确保centos能上网,可以获取到yum源. centos7.2的网络配置: vim /etc/sysconfig/network ...

  7. windows 10开启bash on windows,配置sshd,部署hadoop

    1.安装Bash on Windows 这个参考官网步骤,很容易安装,https://msdn.microsoft.com/en-us/commandline/wsl/install_guide 安装 ...

  8. shell中的变量与eval(转)

    原文链接:http://www.361way.com/shell-eval-variable/4957.html shell 中经常会用到变量的嵌套的情况.比如,单个或多个变量的值作为变量名,再对该变 ...

  9. MySQL 视图、触发器、函数、存储过程

    1. 视图 1.1 什么是视图 通俗来讲,视图就是一条 select 语句执行后返回的结果集.所有我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上. 1.2 视图的特性 视图是对若干张基 ...

  10. jQuery常用事件方法详解

    目录 jQuery事件 ready(fn)|$(function(){}) jQuery.on() jQuery.click jQuery.data() jQuery.submit() jQuery事 ...