尺取法:反复推进区间的开头和结尾,来求取满足条件的最小区间的方法  。    《挑战程序设计》P146

POJ3061

Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15988   Accepted: 6774

Description

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

Sample Input

2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

Sample Output

2
3

题意:求总和不小于S的连续子序列的长度的最小值。

思路:以s(区间左端点)= t(区间右端点)=sum(序列As~At-1的总和)=0初始化,

只要依然有sum<S,就不断将sum增加At,并t++;

如果无法满足sum>=S则循环终止,否则,更新ans=min(ans,t-s);

将sum减去As,s++(更新左端点),继续寻找

CODE:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100010];
int main()
{int T,n,S;scanf("%d",&T);while(T--){scanf("%d%d",&n,&S);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}int s=1,t=1,ans=n+1,sum=0;while(true){while(t<=n&&sum<S){sum+=a[t];t++;}if(sum<S) break;ans=min(ans,t-s);sum-=a[s];s++;}if(ans>n) ans=0;printf("%d\n",ans);}
}

POJ3320

Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13146   Accepted: 4522

Description

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2

题意:一本页数为P的书,每页有一个知识点ai,(同一个知识点可能被多次提到),求需要阅读的最少页数,把所有的知识点都覆盖到

思路:用set求下总知识点数,方法和上题相同尺取法,用map来映射阅读到的知识点次数。

CODE:

#include<stdio.h>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int a[1000005];
int main()
{set<int>st;st.clear();int P;scanf("%d",&P);for(int i=1;i<=P;i++){scanf("%d",&a[i]);st.insert(a[i]);}int all=st.size();int s=1,t=1,sum=0;int ans=P;map<int,int>mp;mp.clear();while(true){while(t<=P&&sum<all){if(mp[a[t]]==0){sum++;}mp[a[t]]++;t++;}if(sum<all) break;ans=min(ans,t-s);mp[a[s]]--;if(mp[a[s]]==0){sum--;}s++;}printf("%d\n",ans);
}

POJ2739

Sum of Consecutive Prime Numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26016   Accepted: 14131

Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime 
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. 
Your mission is to write a program that reports the number of representations for the given positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Sample Output

1
1
2
3
0
0
1
2

题意:求有几段连续素数的和等于n,

思路:2-10000的素数先打表存下来,用尺取法求区间即可

CODE:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int prime[10005],vis[10005],k=0;
void get_prime()
{memset(vis,0,sizeof(vis));for(int i=2;i<=10000;i++){if(!vis[i]){prime[k++]=i;}for(int j=i+i;j<=10000;j+=i){vis[j]=1;}}
}
int main()
{k=0;get_prime();int n;while(~scanf("%d",&n)&&n){int s=0,t=0,sum=0;int ans=0;while(true){while(t<k&&sum<n){sum+=prime[t];t++;}if(sum<n) break;if(sum==n) ans++;sum-=prime[s];s++;}printf("%d\n",ans);}
}

POJ2100

Graveyard Design
Time Limit: 10000MS   Memory Limit: 64000K
Total Submissions: 7518   Accepted: 1864
Case Time Limit: 2000MS

Description

King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves. 
After a consultation with his astrologer, King George decided that the lengths of section sides must be a sequence of successive positive integer numbers. A section with side length s contains s 2 graves. George has estimated the total number of graves that will be located on the graveyard and now wants to know all possible graveyard designs satisfying the condition. You were asked to find them.

Input

Input file contains n --- the number of graves to be located in the graveyard (1 <= n <= 10 14 ).

Output

On the first line of the output file print k --- the number of possible graveyard designs. Next k lines must contain the descriptions of the graveyards. Each line must start with l --- the number of sections in the corresponding graveyard, followed by l integers --- the lengths of section sides (successive positive integer numbers). Output line's in descending order of l.

Sample Input

2030

Sample Output

2
4 21 22 23 24
3 25 26 27

题意:求一个数能由几段连续整数平方的和组成(和2739差不多)

CODE:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long LL;
int a[1000][2];
int main()
{LL n;while(~scanf("%lld",&n)){memset(a,0,sizeof(a));int s=1,t=1;LL sum=0;int ans=0;while(true){while((LL)t*(LL)t<=n&&sum<n)//t是int型所以强制转换下,不然提交结果是超时{sum+=(LL)t*(LL)t;t++;}if(sum<n) break;if(sum==n){a[ans][1]=t;a[ans++][0]=s;}sum-=(LL)s*(LL)s;s++;}printf("%d\n",ans);for(int i=0;i<ans;i++){printf("%d ",a[i][1]-a[i][0]);for(int j=a[i][0];j<a[i][1]-1;j++){printf("%d ",j);}printf("%d\n",a[i][1]-1);}}
}

ACM常用技巧之尺取法--POJ3061/3320/2739/2100相关推荐

  1. ACM常用的解题技巧:尺取法

    常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.之所以需要 ...

  2. ACM常用的解题技巧:尺取法及相关例题

    常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.之所以需要 ...

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

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

  4. ACM 常用技巧 结构体排序 粗粗粗粗讲

    首先说下结构体这种东西,就是,具有同种结构的一堆变量,可以塞到一个叫struct的东西里,声明方法如下.(ps. 别忘了加头文件<cstdlib>)再透露一下,由于本人代码能力菜的一*,所 ...

  5. 尺取法——POJ3061

    #include <iostream> //nlogn复杂度的写法 #include <cstdio> #include <algorithm> using nam ...

  6. ACM尺取法常见题解

    大佬博客地址 常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答 ...

  7. poj3061尺取法/前缀和 二分(java)

    今天遇到这题因为以前没见到过,当时就是想着应该有着一个很简单的方法可以过但是奈何就是没思路.后来看了别人思路写了下来.学习了尺取法 poj3061 题目介绍: Description A sequen ...

  8. 数据结构9-双指针(尺取法)(double pointer)

    双指针有好几种,但是最常用的是尺取法,所以有的时候就说尺取法 双指针,指的是在遍历对象的过程中,不是普通的使用单个指针进行访问,而是使用两个相同方向(快慢指针)或者相反方向(对撞指针)的指针进行扫描, ...

  9. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

最新文章

  1. eclipse问题 - windows版
  2. 关于VMware虚拟机的复制注意事项
  3. linux 卸载 flash,使用率下降到8%,Chrome 87将完全移除Flash
  4. 【机器学习基础知识】各类熵总结
  5. android Tabhost 组件
  6. 深度学习——Optimizer算法学习笔记
  7. java 获取方法的注释_java – 有更有效的方法来获取注释方法吗?
  8. 《从Excel到R 数据分析进阶指南》一第1章 生成数据表1.1 导入数据表
  9. OpenGL基础30:模板测试
  10. cecore.cls.php 08cms,精仿某房产网整站 v1.1
  11. 时间戳转化为YYYY-MM-DD-XX-MM-UU日期格式
  12. word插入页码后只有当前页有页码
  13. Vivado使用技巧(2):综合运行与OOC
  14. 在电脑上打开手机当前浏览的网页
  15. Linux操作系统基础之文件传输
  16. 天籁obd接口针脚定义_汽车OBD接口定义
  17. sql和python还有c语言_TIOBE 4 月排行榜:SQL 进入前十,Python 继续攀升
  18. 小程序开发系列之基础部分-账号注册
  19. 简单粗暴的描述大数据、红海、蓝海、众筹
  20. 51CTO学院 c++视频

热门文章

  1. C#跨线程更新控件(UI)使用delegate方式
  2. 帆软报表嵌入python程序_帆软报表开发步骤
  3. 虚拟机下 linux 连接网络并设置固定ip
  4. 因果图法测试实例:一个处理单价为1元5角钱的盒装饮料的自动售货机软件。
  5. 速龙641电脑装机 硬件 参数 BIOS参数调整
  6. Postman调用grpc接口
  7. vue3组件通信方式
  8. 电脑设置ftp共享文件的方法
  9. 2021年G1工业锅炉司炉最新解析及G1工业锅炉司炉模拟考试
  10. 五一2.74亿人倾巢而出,小长假到底去哪玩儿?