尺取法:反复推进区间的开头和结尾,来求取满足条件的最小区间的方法 。 《挑战程序设计》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);
}
}
}