Subsequence
Description
Input
Output
Sample Input
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int ms=;
int a[ms];
int sum[ms];
int n,S;
void solve()
{
sum[]=;
for(int i=;i<n;i++)
sum[i+]=sum[i]+a[i];
if(sum[n]<S)
{
puts("");
return ;
}
int ans=n;
for(int s=;sum[s]+S<=sum[n];s++)
{
int t=lower_bound(sum+s,sum+n+,sum[s]+S)-sum;
ans=min(ans,t-s);
}
printf("%d\n",ans);
return ;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>S;
for(int i=;i<n;i++)
cin>>a[i];
solve();
}
return ;
}
O(n)
#include"iostream"
#include"cstdio"
#include"algorithm"
#include"cstring"
using namespace std;
const int ms=;
int a[ms];
int n,S;
void solve()
{
int s=,sum=,t=;
int ans=n+;
while()
{
while(t<n&&sum<S)
{
sum+=a[t++];
}
if(sum<S)
break;
ans=min(ans,t-s);
sum-=a[s++];
}
if(ans>n)
{
puts("");
return ;
}
printf("%d\n",ans);
return ;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&S);
for(i=;i<n;i++)
scanf("%d",&a[i]);
solve();
}
return ;
}
Subsequence的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
随机推荐
- Android相关图书推荐
疯狂Android讲义(第3版 附光盘) 作 者 李刚 著 出 版 社 电子工业出版社 出版时间 2015-06-01 版 次 3 页 数 780 印刷时间 2015-0 ...
- Mahout应用(一)
Mahout应用(一) Mahout 是应用于hadoop上的数据挖掘工具(废话不多说) 这里先简单介绍一下mahout的一般使用方法. 拿kmeans为列子 Mahout中的kmeans所需要的输入 ...
- 第二百九十、一、二天 how can I 坚持
上周日,腊八,好冷,去菏泽了.坐火车,没座.好冷,下午菏泽学院,大体看了看,很是怀念,有点遗憾,在网吧玩的时间太长了,留不住. 周一,长太婚礼,竟然没有只是吃了个饭,有点小失望,下午坐车回京,两点坐的 ...
- BUILD_BUG_ON 的解释
知乎上个问题<C 语言有什么奇技淫巧?>排名第一的是一个“抖机灵”的答案. C有一个鲜为人知的运算符叫”趋向于”, 写作“-->”.比如说如果要实现一个倒数的程序,我们可以定义一个变 ...
- Oracle 表数据去重
Oracle数据库中重复数据怎么去除?使用数据表的时候经常会出现重复的数据,那么要怎么删除呢?下面我们就来说一说去除Oracle数据库重复数据的问题.今天我们要说的有两种方法. 一.根据rowid来去 ...
- URAL 2040 Palindromes and Super Abilities 2 (回文自动机)
Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...
- Spring EL Operators example
Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...
- My97DatePicker时间控件使用说明
常规调用: <input id="date01" type="text" onClick="WdatePicker()"/> & ...
- Codeforces Beta Round #7 C. Line (扩展欧几里德)
题目链接:http://codeforces.com/problemset/problem/7/C 给你一个直线方程,有整数解输出答案,否则输出-1. 扩欧模版题.这里有讲解:http://www.c ...
- 超轻量级spring模板方案
最近从事的工作是web方面的,主要j2ee,spring jsp这些内容,由于刚入门,很多的技术都不了解.所谓初生牛犊不怕虎,刚入门,各种不顺手,比如写jsp,总是重复很多的代码,各种不爽,然后就去看 ...