尺取法 poj 2566
尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点。然后一直推进
解决问题的思路:
- 先移动右端点 ,右端点推进的时候一般是加
- 然后推进左端点,左端点一般是减
poj 2566
题意:从数列中找出连续序列,使得和的绝对值与目标数之差最小
思路:
- 在原来的数列开头添加一个0
- 每次找到的区间为 [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
题意:将一个整数分解为连续的素数之和,有多少种分法?
思路:
- 打表,先打出素数表
- 然后依次查询是否满足条件
用尺取法的代码:
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
题意:将一个整数分解为连续数平方之和,有多少种分法?
解决问题的思路:
- 右端点移动,sum+=r*r;
- 如果满足答案就 push (l,r)
- 左端点移动 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的更多相关文章
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- 尺取法 POJ 3601 Subsequence
题目传送门 /* 题意:求连续子序列的和不小于s的长度的最小值 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers
给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点 ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- POJ 2566 Bound Found(尺取法,前缀和)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
- poj 2566 Bound Found 尺取法 变形
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2277 Accepted: 703 Spec ...
- POJ 尺取法
poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...
随机推荐
- Vue.js - Day1
什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App, Vue语法也是可以用于进行手机App开发的,需要借助于We ...
- 解决 MVC4 Code First 数据迁移 数据库发生更改导致调试失败解决方法(二)
文章转载自:http://www.cnblogs.com/amoniyibeizi/p/4486617.html 前几天学MVC过程中,遇到更改Model类以后,运行程序就会出现数据已更改的问题导致调 ...
- C语言问题集
征服C指针:P70#include "stdio.h" char *int_to_str(int int_value){ static char buf[20]; sprintf( ...
- Struts2 displaytag 导出文件为空
以一个user的查询为例,在struts.xml配置里有这个action的配置: 然后可以在UserAction.java里找到list方法这个就是无非new 一个List<User> u ...
- Azure 进阶攻略 | 上云后的系统,「门禁」制度又该如何实现?
各位办公室白领们,不妨回想一下自己每天去公司上班时的一些细节. 为避免「闲杂人等」进入工作场所,我们需要证明自己是这家公司的员工才能进入,对吧!所有员工,无论所属部门或职位,都必须先证明自己身份,例如 ...
- HTML5 data-* 自定义属性操作及其注意点
在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,命名可以用驼峰命名方式,但取值是必需全部使用小写(后面会说),使用这样的结构可以进行数 ...
- Python基础学习之文件(2)
文件内建方法 1.输入 read()方法用来直接读取字节到字符串中,最多读取给定数目个字节.如果没有给定size参数(默认值为-1)或size值为负,文件将被读取直至末尾. readline()方法读 ...
- .net core 操作域控 活动目录 ladp -- Support for System.DirectoryServices for Windows
原文链接:https://github.com/dotnet/corefx/issues/2089 1. @ianhays to kick start the project in CoreFX re ...
- CORS跨域请求的限制和解决
我们模拟一个跨域的请求,一个是8888,一个是8887 //server.js const http = require('http'); const fs = require('fs'); http ...
- 关于VMware给系统分区扩容的一点经验
我的VMware版本是8.0.6 build-1035888,里面安装的是Windows XP SP3 首先,在VM关机状态下使用Hard disk设置中的Utilities下的Expand给整个磁盘 ...