1. 程式人生 > >ACM常用技巧之尺取法--POJ3061/3320/2739/2100

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 s2 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 <= 1014 ).

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);
        }
    }
}