ACM常用技巧之尺取法--POJ3061/3320/2739/2100
尺取法:反复推进区间的开头和结尾,来求取满足条件的最小区间的方法 。 《挑战程序设计》P146
POJ3061
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15988 | Accepted: 6774 |
Description
Input
Output
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
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
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26016 | Accepted: 14131 |
Description
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
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
Time Limit: 10000MS | Memory Limit: 64000K | |
Total Submissions: 7518 | Accepted: 1864 | |
Case Time Limit: 2000MS |
Description
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
Output
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相关推荐
- ACM常用的解题技巧:尺取法
常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.之所以需要 ...
- ACM常用的解题技巧:尺取法及相关例题
常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.之所以需要 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 1 /* 2 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 3 */ 4 #include <cstdio> 5 #include &l ...
- ACM 常用技巧 结构体排序 粗粗粗粗讲
首先说下结构体这种东西,就是,具有同种结构的一堆变量,可以塞到一个叫struct的东西里,声明方法如下.(ps. 别忘了加头文件<cstdlib>)再透露一下,由于本人代码能力菜的一*,所 ...
- 尺取法——POJ3061
#include <iostream> //nlogn复杂度的写法 #include <cstdio> #include <algorithm> using nam ...
- ACM尺取法常见题解
大佬博客地址 常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答 ...
- poj3061尺取法/前缀和 二分(java)
今天遇到这题因为以前没见到过,当时就是想着应该有着一个很简单的方法可以过但是奈何就是没思路.后来看了别人思路写了下来.学习了尺取法 poj3061 题目介绍: Description A sequen ...
- 数据结构9-双指针(尺取法)(double pointer)
双指针有好几种,但是最常用的是尺取法,所以有的时候就说尺取法 双指针,指的是在遍历对象的过程中,不是普通的使用单个指针进行访问,而是使用两个相同方向(快慢指针)或者相反方向(对撞指针)的指针进行扫描, ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
最新文章
- eclipse问题 - windows版
- 关于VMware虚拟机的复制注意事项
- linux 卸载 flash,使用率下降到8%,Chrome 87将完全移除Flash
- 【机器学习基础知识】各类熵总结
- android Tabhost 组件
- 深度学习——Optimizer算法学习笔记
- java 获取方法的注释_java – 有更有效的方法来获取注释方法吗?
- 《从Excel到R 数据分析进阶指南》一第1章 生成数据表1.1 导入数据表
- OpenGL基础30:模板测试
- cecore.cls.php 08cms,精仿某房产网整站 v1.1
- 时间戳转化为YYYY-MM-DD-XX-MM-UU日期格式
- word插入页码后只有当前页有页码
- Vivado使用技巧(2):综合运行与OOC
- 在电脑上打开手机当前浏览的网页
- Linux操作系统基础之文件传输
- 天籁obd接口针脚定义_汽车OBD接口定义
- sql和python还有c语言_TIOBE 4 月排行榜:SQL 进入前十,Python 继续攀升
- 小程序开发系列之基础部分-账号注册
- 简单粗暴的描述大数据、红海、蓝海、众筹
- 51CTO学院 c++视频