poj2566 尺取法
#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 尺取法的更多相关文章
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- 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. ...
- POJ 尺取法
poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
随机推荐
- 51nod 1449 砝码称重(贪心算法)
题目:传送门. 题意:中文题. 题解:左物右码,w进制.m%w==0||m%w==1||m%w==w-1都是可以的,否则是NO. #include <iostream> #include ...
- 使用WKWebView替换UIWebView
开发App的过程中,常常会遇到在App内部加载网页,通常用UIWebView加载.这个自iOS2开始使用的网页加载器一直是开发的心病:加载速度慢,占用内存多,优化困难.如果加载网页多,还可能因为过量占 ...
- struts2封装客户端数据到Action
1.在Action中定义简单数据类型的属性 给Action定义简单类型的属性,封装客户端请求的数据 简单类型:String,基本类型和对应的引用类型 只要保证客户端请求的参数名称和Action的属性名 ...
- Android随笔:属性
android:padding表示给控件的周围加上补白,这样不至于让文本内容会紧靠在边缘上. android:singleLine 设置为true 表示让这个TextView 只能单行显示. andr ...
- [Android Pro] CPU占用计算方法
1: AVTest CPU计算方法读取每个进程的 stat 文件 (/proc/<PID>/stat)计算采样间隔10min下utime的差值minusUtime,stime的差值min ...
- SQL语句简介
1.Top.Distinct Top 获取前几条数据,top一般都与order by连用 获得年纪最小的5个学生 select top 5 * from A order by classesId 获 ...
- INNODB
INNODB,是Mysql5.7的默认存储引擎,是事务安全的,支持ACID,具有提交,回滚和crash-recovery[灾备]能力,以保护用户数据. 优势:一旦Server崩溃,Innodb会自动保 ...
- 当ListView有Header时,onItemClick里的position不正确
原文:http://blog.chengbo.net/2012/03/09/onitemclick-return-wrong-position-when-listview-has-headerview ...
- 设计模式学习之代理模式(Proxy,结构型模式)(11)
参考地址:http://www.cnblogs.com/zhili/p/ProxyPattern.html 一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访 ...
- Pyqt phonon的使用
本文是用Pyqt实现了下网上一个Qt版大牛关于phonon的介绍 Qt phonon地址:http://wenku.baidu.com/link?url=nH_dZ8lZbXHy8N5__8jAWLX ...