尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间。

 

poj3061 http://poj.org/problem?id=3061

给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的长度的最小值

如果序列尺取法 poj3061 poj3320   是总和最迟大于S的连续子序列

那么 尺取法 poj3061 poj3320

所以只有加上尺取法 poj3061 poj3320, 从尺取法 poj3061 poj3320开始的连续子序列才有可能大于S

所以从尺取法 poj3061 poj3320开始的总和最初大于S的连续子序列是尺取法 poj3061 poj3320则一定有尺取法 poj3061 poj3320

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <string>
12 #include <math.h>
13 using namespace std;
14 #pragma warning(disable:4996)
15 typedef long long LL;
16 #define cinInt(a) scanf("%d",&a)
17 #define cinInt64(a) scanf("%I64d",&a)
18 #define cinDouble(a) scanf("%lf",&a)
19 const int INF = 1 << 30;
20 const int N = 100000 + 10;
21 int a[N];
22 void input(int &x)
23 {
24     char ch = getchar();
25     while(ch>'9' || ch<'0')
26         ch = getchar();
27     x = 0;
28     while(ch>='0' && ch<='9')
29     {
30         x = x * 10 + ch - '0';
31         ch = getchar();
32     }
33 }
34 int main()
35 {
36     int n,S,i;
37     int t;
38     scanf("%d",&t);
39     while(t--)
40     {
41         scanf("%d%d",&n,&S);
42         for(i=0; i<n; ++i)
43         {
44             //scanf("%d",&a[i]);
45             input(a[i]);
46         }
47         int ans = INF;
48         int s = 0, t = 0,sum =0;
49         for(;;)
50         {
51             while(t<n && sum<S)
52             {
53                 sum += a[t++];
54             }
55             if(sum<S) break;
56             ans = min(ans,t-s);
57             sum -= a[s++];
58         }
59         if(ans==INF)
60             puts("0");
61         else
62             printf("%d\n",ans);
63     }
64     return 0;
65 }
View Code

相关文章:

  • 2021-07-22
  • 2022-12-23
  • 2021-05-27
  • 2021-09-11
  • 2022-02-23
猜你喜欢
  • 2021-08-19
  • 2021-07-08
  • 2022-12-23
  • 2021-08-19
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案