poj 3061 Subsequence
2016-06-08 21:34
309 查看
题意:n个数,求最短的子序列和>=s
分析:尺取法,也叫蠕虫法,每次枚举起点,然后往右移动,找到右边界
View Code
分析:尺取法,也叫蠕虫法,每次枚举起点,然后往右移动,找到右边界
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int maxn=1e5+5; int c[maxn]; int main(){ int t,n,s; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&s); for(int i=0;i<n;i++) scanf("%d",c+i); int ans=n+1; int l=0,r=0,sum=0; for(;;){ while(r<n&&sum<s) sum+=c[r++]; if(sum<s)break; ans=min(ans,r-l); sum-=c[l++]; } if(ans==n+1) puts("0"); else printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- ( Leetcode 95 ) Unique Binary Search Trees II
- UIImagePickerController.h SDK 翻译
- UILabel 高度的自适应与获取
- QtQuick折腾结论之model delegate 的正确使用姿势
- IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop
- QtQuick排坑(不定时更新)
- POJ 2524 Ubiquitous Religions
- 2016 UESTC Training for Math Problem E- 蹲在墙角画圈圈的愚人王
- LeetCode:N-Queens
- 在Qt中生成UUID(全球唯一码)
- vue 多项目 依赖webpack开发共用 配置
- poj1458Common Subsequence--LCS
- LeetCode:N-Queens II
- 有关StringBuilder字符串的一些应用
- Looper、Message、MessageQueue、Handler异步消息处理机制源码分析
- 检测预设资源是否有UIFont为空的编辑器
- Android学习之图解调用invalidate()和requestLayout()的过程
- Parquet 读写
- 栈(stack)和队列(queue)的简单实例
- UIAlertController