【题目大意】

给出一个整数列,求一段子序列之和最接近所给出的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. cmd 使用gii的命令行用法

    1.生成模型 php yii gii/model --ns=common\models --tableName=contract_supplemental --modelClass=ContractS ...

  2. Spring Boot中使用MongoDB数据库

    前段时间分享了关于Spring Boot中使用Redis的文章,除了Redis之后,我们在互联网产品中还经常会用到另外一款著名的NoSQL数据库MongoDB. 下面就来简单介绍一下MongoDB,并 ...

  3. 手机网页的头部meta的相关配置~~

    今天使用sublime写手机端网页的时候,发现木有meta的自动生成手机网页的快捷键·~ 然后就去网上巴拉,准备存储一份~~哈哈 一.天猫 <title>天猫触屏版</title&g ...

  4. Pyrhon代码的中文问题

    解决代码中出现中文乱码的问题: 使用中文需要在第一行声明编码#encoding=utf-8 或者#coding=utf-8 python只检查#.coding和编码字符串,所以你可能回见到下面的声明方 ...

  5. tar解压与压缩

    1.解压 tar -zxvf  压缩文件名  -C 指定的目录   (制定的目录必须存在) 2.压缩 tar -czvf  压缩后的文件名   要压缩的文件夹

  6. Mysql储存过程1: 设置结束符与储存过程创建

    #显示储存过程 show procedure status; #设置结束符 delimiter $; #创建储存过程 create procedure procedure_name() begin - ...

  7. vsftpd 安装配置详细教程

    linux下ftp软件不少,大致特点:<br /> wu-ftp:比较老牌,但针对它的攻击比较多,设置比较麻烦,但功能比较强大.<br /> vsftpd:功能强大,配置也比较 ...

  8. C++ 内联函数inline

    http://blog.csdn.net/u011327981/article/details/50601800 1.  内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码 ...

  9. Power Profiles for Android

    http://source.android.com/devices/tech/power.html Battery usage information is derived from battery ...

  10. openjudge-NOI 2.6-1759 最长上升子序列

    题目链接:http://noi.openjudge.cn/ch0206/1759/ 题解: 奇怪……之前博客里的o(nlogn)标程在codevs和tyvj上都能AC,偏偏它这里不行 #include ...