POJ 3061 Subsequence 尺取

 1 //第一个答案2(5+10) 第二个 3(3+4+5)
 2 //令 sum[i]=a0+a1+...+ai,  as+...+a(t-1)>=S 
 3 //有 as+a(s+1)+...+a(t-1)=sum[t]-sum[s] 
 4 //则 ai+...+an=sum[n]-sum[i]>=S
 5 //所以问题转化成S+sum[i]在sum找 
 6 #include<cstdio>
 7 #include<iostream>
 8 #include<algorithm>
 9 #include<queue>
10 #include<map>
11 #include<vector>
12 #include<set>
13 #include<string>
14 #include<cmath>
15 #include<cstring>
16 using namespace std;
17 int a[100010];
18 int s;
19 int main()
20 {
21     int sum[100010];
22     int cnt;
23     scanf("%d",&cnt);
24     while(cnt--)
25     {
26         int n,S;
27         scanf("%d%d",&n,&S);
28         for(int i=0;i<n;i++)
29         {
30             scanf("%d",&a[i]);
31         }
32         
33         int sum=0,s=0,t=0;
34         int res=n;
35         while(true)
36         {
37             while(t<n&&sum<=S)
38             {
39                 sum+=a[t++];
40             }
41             if(sum<S)//找完一圈没有就不找了 
42                 break;
43             res=min(res,t-s);
44             sum-=a[s++];
45         }
46         printf("%d\n",res);
47     } 
48     return 0;
49 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值