WOJ1301-Subsequence
来源:互联网 发布:三菱plc 编程手册 编辑:程序博客网 时间:2024/05/22 10:43
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.
输入格式
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.
输出格式
For each the case the program has to print the result on separate line of the output file.if no answer, print 0.
样例输入
210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5
样例输出
23
#include<stdio.h>int main(){ int t; scanf("%d",&t); while(t--){ int n,s,i,j=0,ans=0,length=0,sum=0,flag=0; int in[100000]; scanf("%d %d",&n,&s); for(i=0;i<n;i++) scanf("%d",&in[i]); for(i=0;j<n;){ if(i==j) sum=in[i]; else{ if(flag) sum=sum+in[j]; else sum=sum-in[i-1]; } if(sum>=s){ flag=0; length=j-i+1; if(ans==0) ans=length; else if(length<ans) ans=length; } else flag=1; if(ans==1) break; if(flag) j++; else i++; } printf("%d\n",ans); } return 0;}
- WOJ1301-Subsequence
- Subsequence
- Subsequence
- Subsequence
- subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- Subsequence
- LeetCode: -Dynamic Programming-Maximum Subarray[53]-子数组最大和
- 【006】Java语言(命令行简介及一些基本操作)
- Fugitive.vim 使用教程二
- Mysql知识总结
- 解决eclipse中java代码注释变成乱码的问题
- WOJ1301-Subsequence
- 四、Spring Cloud 的 Feign 组件
- JQuery返回Json日期格式的问题/Date(1387900800000)/
- 实训有感
- 17.9.9晨报
- nuget.server搭建及实际测试
- 数据分析笔试
- 练习 > CTF解题 > OpenCTF 2017 > variacover(变量覆盖和MD5绕过)
- STM32配置外设寄存器,不论怎么写都写不进去或全是0.