Subsequence
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9587 | Accepted: 3855 |
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
题意:给定长度为n的数列整数A0,A1,A2……An-1以及整数S。求出总和不小于S的连续子序列的长度的最小值。如果解不存在,则输出0。
分析:尺取法。只要sum<S,则一直往后加Ai;当sum>=S时,一直去掉前面已加的项。
题目链接:http://poj.org/problem?id=3061
代码清单:
#include<iostream> #include<algorithm> using namespace std; const int maxn = 100000 + 5; int N,S,T; int a[maxn]; void input(){ cin>>N>>S; for(int i=0;i<N;i++) cin>>a[i]; } //尺取法 void solve(){ int sum=0,Start=0,End=0,ans=maxn; while(true){ while(End<N&&sum<S){ //当总和sum<S时,一直往后加 sum+=a[End]; End++; } if(sum<S) break; while(Start<N&&sum>=S){ //当总和sum>=S时,一直去掉前面的项 sum-=a[Start]; Start++; } ans=min(ans,End-Start+1); } if(ans>N) cout<<"0"<<endl; else cout<<ans<<endl; } int main(){ cin>>T; while(T--){ input(); solve(); }return 0; }
Jessica‘s Reading Problem
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7838 | Accepted: 2507 |
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
题意:为了准备考试,Jessica开始读一本很厚的课本。要想通过考试,必须把课本中所有的知识点都掌握。这本书共有P(1<=P<=10^6)页,第i页恰好有一个知识点Ai(每个知识点都有一个整数编号)。全书中同一个知识点可能会被多次提到,所以她希望通过阅读其中连续的一些页把所有的知识点都覆盖到。给定每页写到的知识点,请求出要阅读的最少页数。
分析:尺取法。我们可以先把种类数算出来(利用set集合),然后,假设在区间[s,t]已经覆盖了所有的知识点,我们可以从s开始,把s取走后,那么页s上的知识点出现次数就要减一,如果此时这个知识点的出现次数为0了,那么,在同一个知识点出现之前,不停地将区间末尾t向后推进即可。
题目链接:http://poj.org/problem?id=3320
代码清单:
#include<set> #include<map> #include<iostream> #include<algorithm> using namespace std; const int maxn = 1000000 + 5; int p,a[maxn]; set<int>all; map<int,int>m; void input(){ scanf("%d",&p); for(int i=0;i<p;i++){ scanf("%d",&a[i]); all.insert(a[i]); m[a[i]]=0; } } void solve(){ int Start=0,End=0,ans=maxn; int n=all.size(),sum=0; while(true){ while(End<p&&sum<n){ if(m[a[End]]==0){ sum++; } m[a[End]]++; End++; } if(sum<n) break; ans=min(ans,End-Start); if(m[a[Start]]==1){ sum--; } m[a[Start]]--; Start++; } printf("%d\n",ans); } int main(){ input(); solve(); return 0; }