ACM常用技巧之尺取法--POJ3061/3320/2739/2100


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值