ACM PKU 3061 Subsequence
http://acm.pku.edu.cn/JudgeOnline/problem?id=3061
Subsequence Time Limit:1000MS Memory Limit:65536K
Total Submit:2626 Accepted:833
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
210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5
Sample Output
23
Source
Southeastern Europe 2006
不知道為什么這道題在Discuss里被罵得體無完膚
http://acm.pku.edu.cn/JudgeOnline/bbs?problem_id=3061
注意細節很重要啊!我至少調試了兩個小時才AC!!
Source
Problem Id:3061 User Id:lnmm Memory:464K Time:31MS
Language:C++ Result:Accepted
Source
Total Submit:2626 Accepted:833
Description
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
210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5
Sample Output
23
Source
Southeastern Europe 2006
不知道為什么這道題在Discuss里被罵得體無完膚
http://acm.pku.edu.cn/JudgeOnline/bbs?problem_id=3061
注意細節很重要啊!我至少調試了兩個小時才AC!!
Language:C++ Result:Accepted
Source
1
#include"stdio.h"
2
int a[100010];
3
void main()
4

{
5
long sum,N,S,min;
6
long left,right,r; //left 左游標,right 右游標, r向右擴展游標
7
int T,i;
8
scanf("%d",&T);
9
for(i=1;i<=T;i++)
10
{
11
sum=0;
12
scanf("%ld%ld",&N,&S);
13
for(r=1;r<=N;r++)
14
{
15
scanf("%ld",&a[r]);
16
sum+=a[r];
17
}
18
min=100001;
19
if(sum<S)
20
{
21
min=0;
22
}
23
sum=0;
24
right=0;
25
a[0]=0;
26
//初始化完成
27
28
29
for(left=1;left<=N;left++)
30
{
31
sum=sum-a[left-1];
32
if(sum >= S)
33
{
34
if(right-left+1 < min ) min=right-left+1;
35
continue;
36
}
37
for(r=right+1;r<=N;r++)
38
{
39
sum=sum+a[r];
40
if(sum>=S)
41
{ if(r-left+1 < min) min=r-left+1;
42
right=r;
43
break;
44
}
45
}
46
}
47
48
printf("%d\n",min);
49
}
50
51
return ;
52
}
#include"stdio.h" 2
int a[100010]; 3
void main() 4


{ 5
long sum,N,S,min; 6
long left,right,r; //left 左游標,right 右游標, r向右擴展游標 7
int T,i; 8
scanf("%d",&T); 9
for(i=1;i<=T;i++) 10

{ 11
sum=0; 12
scanf("%ld%ld",&N,&S); 13
for(r=1;r<=N;r++) 14

{ 15
scanf("%ld",&a[r]); 16
sum+=a[r]; 17
} 18
min=100001; 19
if(sum<S) 20

{ 21
min=0; 22
} 23
sum=0; 24
right=0; 25
a[0]=0; 26
//初始化完成 27

28

29
for(left=1;left<=N;left++) 30

{ 31
sum=sum-a[left-1]; 32
if(sum >= S) 33

{ 34
if(right-left+1 < min ) min=right-left+1; 35
continue; 36
} 37
for(r=right+1;r<=N;r++) 38

{ 39
sum=sum+a[r]; 40
if(sum>=S) 41

{ if(r-left+1 < min) min=r-left+1; 42
right=r; 43
break; 44
} 45
} 46
} 47

48
printf("%d\n",min); 49
} 50

51
return ; 52
}

