【尺取法好题】POJ2566-Bound Found
【题目大意】
给出一个整数列,求一段子序列之和最接近所给出的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的更多相关文章
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
- hdu 5056(尺取法思路题)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 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 ...
- codeforces #364c They Are Everywhere 尺取法
C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Bound Found [POJ2566] [尺取法]
题意 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. Input The input file contains several test cases. Each t ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- POJ 2566 Bound Found(尺取法,前缀和)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
随机推荐
- 判断是否引入jquery
主要使用typeof检验 <script language="javascript"> if(typeof jQuery == 'undefined'){ window ...
- IE6透明PNG解决方案
IE6不支持PNG-24图片一直困扰很多人,但是可以通过IE的独有的滤镜来解决,解决的方案很多,比如:将滤镜写在CSS里,还可以写成单独的 Javascript文件,本来认为推荐两种做法:第一种,将所 ...
- python爬虫多线程编程
#使用了线程库 import threading from queue import Queue from bs4 import BeautifulSoup import json import re ...
- Python递归 — — 二分查找、斐波那契数列、三级菜单
一.二分查找 二分查找也称之为折半查找,二分查找要求线性表(存储结构)必须采用顺序存储结构,而且表中元素顺序排列. 二分查找: 1.首先,将表中间位置的元素与被查找元素比较,如果两者相等,查找结束,否 ...
- windows 10添加定时任务
1.在搜索栏搜索‘任务计划’ 2.选择任务计划程序,打开 3.创建基本任务 4.输入任务名称 5.选择任务触发周期 6.选择任务触发的具体时间点 7.选择任务需要做的事 8.选择启动程序后,选择具体的 ...
- IP负载均衡技术
参考链接:http://www.360doc.com/content/12/1117/19/820209_248442094.shtml
- Linux下不能挂载NTFS格式硬盘/U盘
如果大家以后在Ubuntu系统下面遇到NTFS格式的移动硬盘哪个分区不能挂载的话,可以尝试sudo ntfsfix /dev/你相应的分区
- 数据库连接池(c3p0与druid)
1.数据库连接池概念 其实就是一个容器(集合),存放数据库连接的容器.当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归 ...
- 快速地从Redhat系转Ubuntu系
ubuntu官网的,https://help.ubuntu.com/community/SwitchingToUbuntu/FromLinux/RedHatEnterpriseLinuxAndFedo ...
- 服务器或普通PC裸机安装 ESXI6.5
ESXI :安装包 http://pan.baidu.com/s/1c2gM0Xq (包含注册机和其他套件,驱动打包工具) ESXI 6.5 在服务器安装比较方便,一般intel 的网卡都没多大问题, ...