LA2678 - Subsequence(求序列中长度最短的和>=S的子序列长度)
来源:互联网 发布:ubuntu命令行设置中文 编辑:程序博客网 时间:2024/05/10 18:55
自己看完题目敲了。。
给出n s
然后n个数的序列
要从序列中找子序列 使得和大于s 要序列长度最小 求这个长度
这题目 看了就想到双指针了 敲后过了 在注释里写思想吧
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=100000+10;int arr[maxn];int main(){ //freopen("/home/gl/acm/in","r",stdin); int n,s; while(scanf("%d %d",&n,&s)!=EOF) { for(int i=0;i<n;++i) scanf("%d",&arr[i]); int sum=0,ans=n+1,index1=0,index2=0; while(sum<s && index2<n)//先找到从起点开始 和大于s的那个index2 得到index2 之前的所有和大于s { sum+=arr[index2++]; } if(sum<s)//这种情况就是index2到头了 还没有大与等于s 那就没答案了 { printf("0\n"); continue; } ans=index2;//ans表示长度 while(index1<index2 && index2<=n)//index2 等于n是可以的 表示到头了index 1不能等于index2 { sum-=arr[index1++];//每次index1 向前移动 sum减去index1那的值 if(sum>=s){//如果还满足 更新ans ans=min(ans,index2-index1); } else{//不满足了 就得找满足的条件了 index2 又得往前走了 while(sum<s && index2<=n) sum+=arr[index2++]; if(sum<s) break; else ans=min(ans,index2-index1); } } printf("%d\n",ans); } return 0;}
- LA2678 - Subsequence(求序列中长度最短的和>=S的子序列长度)
- 求子序列的长度
- 求最长递增子序列的长度
- 求最长上升子序列的长度
- 求最长上升子序列的长度
- 求最长公共子序列的长度
- 求最大公共子序列的长度
- 求最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- (hdu step 3.2.2)Common Subsequence(简单dp:求最长公共子序列的长度)
- [LeetCode] Longest Increasing Subsequence 最长递增子序列的长度
- 求数组的最长递增子序列的长度
- SEERC 2006 / UVa 1121 Subsequence (有和下界的最短连续子序列)
- UICollectionView
- 值类型和引用类型区别
- 如何减小与“大牛”的差距
- 判断学生成绩水平
- ssl双向认证
- LA2678 - Subsequence(求序列中长度最短的和>=S的子序列长度)
- 数据结构:静态链表
- use map<key, val> in C++, solve LeetCode Anagrams 240 ms pass large test set
- Collection 和 Collections的区别
- javascript 取整
- ssh plink 端口转发
- rhel AS5.4相关
- 欧拉函数
- ubuntu deb包制作例程