POJ 3061 - Subsequence
2016-05-12 20:53
423 查看
题意:给你N个数,求总和不小于s的连续子序列的最小长度。
分析:从头开始先选出大于等于s的子序列和,然后减去该子序列左边的值,如果小于s,则子序列友继续往后加,大于s则,继续减去左边的值。
分析:从头开始先选出大于等于s的子序列和,然后减去该子序列左边的值,如果小于s,则子序列友继续往后加,大于s则,继续减去左边的值。
#include <iostream> #include<cstdio> using namespace std; #define N 100005 int a ; int main() { int m; scanf("%d",&m); while(m--) { int n,s; scanf("%d%d",&n,&s); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } int st=0,t=0,sum=0; int ans=n+1; while(1) { while(t<n&&sum<s) { sum+=a[t++]; } if(sum<s) break; ans=min(ans,t-st); sum-=a[st++]; } if(ans>n) { ans=0; } printf("%d\n",ans); } return 0; }
相关文章推荐
- StringBuilder的使用
- ugui 添加事件的几种方式
- Guid的使用
- StringBuilder的使用
- layer.layui.com 一个不错的弹窗层js插件库
- UIscrollView
- 1711 Number Sequence(kmp)
- Android的线程使用来更新UI------Thread Handler Looper TimerTask等
- 303. Range Sum Query - Immutable
- Adnroid5.1 最近应用分析(systemui-recents)
- vue-cli学习开发总结
- easyui --单元格编辑
- Partition-方案二:通过 Insert with a subquery 方法
- android UI——跑马灯TextView
- iOS UIKit:TableView之编辑模式(3)
- Android 查询远程服务器的工具QueryUtils
- UI layout 优化
- UI 增加热点区域
- AndroidStudio导入项目一直Building解决方案
- c# 解析json 字符串 报异常 Bad JSON escape sequence 解决方案