尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点。然后一直推进

解决问题的思路:

  1. 先移动右端点 ,右端点推进的时候一般是加
  2. 然后推进左端点,左端点一般是减

poj 2566

题意:从数列中找出连续序列,使得和的绝对值与目标数之差最小

思路:

  1. 在原来的数列开头添加一个0
  2. 每次找到的区间为 [min(i,j)+1,max(i,j)]

应用尺取法的代码:

 while (r <=n)
{
int sub = pre[r].sum - pre[l].sum;
while (abs(sub - t) < Min)
{
Min = abs(sub - t);
ansl= min(pre[l].id, pre[r].id) + ;
ansr = max(pre[l].id, pre[r].id);
ans = sub;
}
if (sub < t)
r++;
else if (sub > t) l++;
else break;
if (l == r) r++;
}

解决问题的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e5 + ;
const int INF = 0x7fffffff;
int n, k;
int a[N];
struct node {
int sum, id;
}pre[N];
bool cmp(node a, node b)
{
return a.sum < b.sum;
}
int main()
{
while (scanf("%d%d", &n, &k) != EOF)
{
if (n == && k == ) break;
pre[].id = , pre[].sum = ;
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
pre[i].id = i;
pre[i].sum = pre[i - ].sum + a[i];
}
sort(pre, pre + n + , cmp);
for (int i = ; i < k; i++)
{
int t;
scanf("%d", &t);
int Min = INF;
int l = , r = ;
int ans, ansl, ansr;
while (r <=n)
{
int sub = pre[r].sum - pre[l].sum;
while (abs(sub - t) < Min)
{
Min = abs(sub - t);
ansl= min(pre[l].id, pre[r].id) + ;
ansr = max(pre[l].id, pre[r].id);
ans = sub;
}
if (sub < t)
r++;
else if (sub > t) l++;
else break;
if (l == r) r++;
}
printf("%d %d %d\n", ans, ansl, ansr);
}
}
return ;
}

poj 2739

题意:将一个整数分解为连续的素数之和,有多少种分法?

思路:

  1. 打表,先打出素数表
  2. 然后依次查询是否满足条件

用尺取法的代码:

 for (;;) {
while (r < size&&sum < n)
{
sum += primes[r++];
}
if (sum < n) break;
else if (sum == n) result++;
sum -= primes[l++];
}

解决问题的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define maxn 10000+16
vector<int> primes;
vector<bool> is_prime;
void init_prime()
{
is_prime = vector<bool>(maxn + , true);
is_prime[] = is_prime[] = false;
for (int i = ; i < maxn; i++)
{
if (is_prime[i])
{
primes.push_back(i);
for (int j = i * ; j < maxn; j += i)
{
is_prime[j] = false;
}
}
}
}
int main()
{
int n;
init_prime();
int size = primes.size();
while (cin >> n && n)
{
int result = , sum = ;
int l = ,r = ;
for (;;) {
while (r < size&&sum < n)
{
sum += primes[r++];
}
if (sum < n) break;
else if (sum == n) result++;
sum -= primes[l++];
}
printf("%d\n", result);
}
return ;
}

poj 2100

题意:将一个整数分解为连续数平方之和,有多少种分法?

解决问题的思路:

  1. 右端点移动,sum+=r*r;
  2. 如果满足答案就 push (l,r)
  3. 左端点移动 sum-=l*l;

用尺取法的代码:

 for (;;)
{
while (sum < n)
{
sq = r * r;
sum += sq;
r++;
}
if (sq > n) break;
else if (sum == n) ans.push_back(make_pair(l, r));
sum -= l * l;
l++;
}

解决问题的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <vector>
#include <queue>
using namespace std; typedef long long ll;
vector<pair<ll, ll>> ans;
void solve(ll n)
{
ll l = , r = , sum = , sq;
for (;;)
{
while (sum < n)
{
sq = r * r;
sum += sq;
r++;
}
if (sq > n) break;
else if (sum == n) ans.push_back(make_pair(l, r));
sum -= l * l;
l++;
}
ll size = ans.size();
printf("%lld\n", size);
for (ll i = ; i < size; i++)
{
ll ansr = ans[i].second;
ll ansl = ans[i].first;
printf("%lld",ansr-ansl);
for (ll j=ansl;j<ansr;j++)
printf(" %lld", j);
printf("\n");
}
} int main()
{
ll n;
while (scanf("%lld", &n) != EOF)
{
if (n == ) break;
solve(n);
}
return ;
}

尺取法 poj 2566的更多相关文章

  1. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  2. 尺取法 POJ 3601 Subsequence

    题目传送门 /* 题意:求连续子序列的和不小于s的长度的最小值 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] ...

  3. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  4. 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers

    给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点               ...

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

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

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

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

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

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

  8. poj 2566 Bound Found 尺取法 变形

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2277   Accepted: 703   Spec ...

  9. POJ 尺取法

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

随机推荐

  1. css颜色 hsla 和line-gradient

    h 表示色调 从0-360 s 饱和度  0 - 100% l 亮度    0 -100% a 透明度  0-1

  2. BizMDM企业主数据管理平台

    类型: 定制服务 软件包: business intelligence integrated industry solution collateral 联系服务商 产品详情 解决方案 概要 在全新的数 ...

  3. UDoc(云平台企业应用级 文档管理产品)

    类型: 定制服务 软件包: integrated industry solution collateral 联系服务商 产品详情 解决方案 概要 为企业提供基于云平台企业应用级文档管理产品,尽可能最大 ...

  4. SAP ECC6.0-中建信息版

    平台: SUSE Enterprise Server 类型: 虚拟机镜像 软件包: sap ecc 6.0 ehp7 sps10 commercial erp sap 服务优惠价: 按服务商许可协议 ...

  5. 笨办法学Python(二十八)

    习题 28: 布尔表达式练习 上一节你学到的逻辑组合的正式名称是“布尔逻辑表达式(boolean logic expression)”.在编程中,布尔逻辑可以说是无处不在.它们是计算机运算的基础和重要 ...

  6. April 7 2017 Week 14 Friday

    A good heart is better than all the brains in the world. 聪明绝顶,不如宅心仁厚. A good heart can be useful to ...

  7. CUDA Texture纹理存储器 示例程序

    原文链接 /* * Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved. * Data: 2012-4-20 */ // // 此程序是演示了1D和 ...

  8. CUDA中多维数组以及多维纹理内存的使用

    纹理存储器(texture memory)是一种只读存储器,由GPU用于纹理渲染的图形专用单元发展而来,因此也提供了一些特殊功能.纹理存储器中的数据位于显存,但可以通过纹理缓存加速读取.在纹理存储器中 ...

  9. Laplace算子和Laplacian矩阵

    1 Laplace算子的物理意义 Laplace算子的定义为梯度的散度. 在Cartesian坐标系下也可表示为: 或者,它是Hessian矩阵的迹: 以热传导方程为例,因为热流与温度的梯度成正比,那 ...

  10. using System.Security.Cryptography

    这个命名空间主要是用来进行加密的一些类. 加密服务: 公共网络(如 Internet)不提供实体之间安全通信的方式. 此类网络上的通信易被读取或甚至被未经授权的第三方修改. 加密有助于防止数据被查看, ...