poj 3061 Subsequence & 3220 Jessica's Reading Problem

原题:
Subsequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21135 Accepted: 9032
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
Source

Southeastern Europe 2006

中文:

给你n个整数和一个整数s,让你算出总和不小于s的连续子序列长度的最小值,没有解则输出0。

代码:

//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>

using namespace std;
typedef long long ll;


int a[100001],n,up;
int k=0;
int main()
{
    ios::sync_with_stdio(false);
    cin>>k;
    while(k--)
    {
        cin>>n>>up;
        for(int i=0;i<n;i++)
            cin>>a[i];

        int sum=0,ans=n+1,s=0,t=0;

        while(true)
        {
            while(t<n&&sum<up)
            {
                sum+=a[t++];
            }
            if(sum<up)
                break;

            ans=min(ans,t-s);

            sum-=a[s++];

        }
        if(ans>n)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}

解答:

挑战程序设计竞赛上面的例题,使用尺取法求解,算法的时间复杂度是O(n)。

详见p148

原题:
Jessica’s Reading Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17020 Accepted: 5891
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
Source

POJ Monthly–2007.08.05, Jerry

中文:

给你n个数,让你找出一个最短的连续区间段,使得这个区间段中包含所有这n个数组成的集合。

代码:

//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>

using namespace std;
typedef long long ll;

map<int,int> mi;

int a[1000001],n;
set<int> si;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        si.clear();
        mi.clear();
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            si.insert(a[i]);
        }
        int num=si.size();
        int cnt=0,ans=n+1,s=0,t=0;

        while(true)
        {
            while(t<n&&cnt<num)
            {

                if(mi[a[t++]]++ == 0)
                    cnt++;
            }

            if(cnt<num)
                break;

            ans=min(ans,t-s);

            if(--mi[a[s++]]==0)
                cnt--;

        }
        cout<<ans<<endl;
    }
    return 0;
}


解答:

有上面一道题的铺垫,此题应该很好想。首先下标t一直向前走,每次使用map保存遍历过的数及出现的次数,直到包含了集合中所有的数后停下。然后下标s向前滚动,类似滑动窗口的方法,将向前滚动过的数从map中剔除。每次更新窗口长度即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值