Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10875   Accepted: 4493

Description

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.

Input

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.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

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
题解:让求连续的一个序列数之和大于等于S的最短序列长度;这道题,我前后换了三种方法才A了,刚开始看到,一想不就是个线段树,写完了发现答案不对。。。然后发现线段树只能找到一半,
还呆加上区间合并,区间合并也很可能不对,然后想着树状数组,写了一半感觉还不如用个数组直接存到i的总值和,然后找到起点终点就好了,于是开始了暴力,暴力肯定超时啊;就想着二分下;
调试了下就过了;二分还要判断下当前点与前一个点插哪个;
可能我写的太麻烦了。。。有空看看大神怎么写的;
AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
const int INF=0x3f3f3f3f;
const int MAXN=;
int tree[MAXN]; int main(){
int T,N,M;
SI(T);
while(T--){
SI(N);SI(M);
mem(tree,);
int ans=INF;
int t=;
for(int i=;i<N;i++){
int x;
SI(x);
if(!i)tree[i]=x;
else tree[i]=tree[i-]+x;
}
for(int i=N-;i>=;i--){
if(tree[i]-M>=){
int t=lower_bound(tree,tree+i,tree[i]-M)-tree;
if(tree[i]-tree[t]>=M)ans=min(ans,i-t);
else ans=min(ans,i-t+);
//printf("%d\n",ans);
}
}
if(ans==INF)puts("");
else printf("%d\n",ans);
}
return ;
}
// handsomecui.cpp : 定义控制台应用程序的入口点。
// //#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = ;
LL seq[MAXN];
int erfen(int l, int r, int v){
int mid;
while(l <= r){
mid = (l + r) >> ;
if(seq[mid] >= v)
r = mid - ;
else
l = mid + ;
}
return r + ;
}
int main()
{
int N, S, T;
cin >> T;
while(T--){
cin >> N >> S;
int ans = 0x3f3f3f3f;
memset(seq, , sizeof(seq));
for(int i = ; i < N; i++){
cin >> seq[i];
if(i)
seq[i] += seq[i - ];
}
for(int i = ; i < N; i++){
if(seq[i] - S < )
continue;
int p = erfen(, i - , seq[i] - S);
//if(p < 0 || p >= i)continue;
if(seq[p] + S > seq[i])p--;
ans = min(ans, i - p);
}
if(ans == 0x3f3f3f3f)
puts("");
else
printf("%d\n",ans);
}
return ;
}

Subsequence(暴力+二分)的更多相关文章

  1. poj3977 - subset - the second time - 暴力 + 二分

    2017-08-26 11:38:42 writer:pprp 已经是第二次写这个题了,但是还是出了很多毛病 先给出AC代码: 解题思路: 之前在培训的时候只是笼统的讲了讲怎么做,进行二分对其中一边进 ...

  2. Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)

    A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. Vijos P1116 一元三次方程求解【多解,暴力,二分】

    一元三次方程求解 描述 有形如:ax^3+bx^2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之 ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力

    D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...

  5. I Count Two Three HDU - 5878(暴力二分)

    为甚么16年Qingdao Online 都是暴力题emm///... 先暴力预处理 然后lower _bound二分 #include <iostream> #include <c ...

  6. HDU6127 简单几何 暴力二分

    LINK 题意:给出n个点,每个点有个权值,可以和任意另外一点构成线段,值为权值积.现问过原点的直线中交所有线段的权值和的最大值,注意直线必不经过点. 思路:直线可以将点集分为两侧,此时的权值为两侧点 ...

  7. Codeforces Beta Round #3 B. Lorry 暴力 二分

    B. Lorry 题目连接: http://www.codeforces.com/contest/3/problem/B Description A group of tourists is goin ...

  8. Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分

    D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...

  9. 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分

    E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...

随机推荐

  1. JavaScript实现私有属性

    原文:JavaScript实现私有属性 JavaScript被很多人认为并不是一种面向对象语言,原因有很多种,比如JavaScript没有类,不能提供传统的类式继承:再比如JavaScript不能实现 ...

  2. linux监控脚本,脚本支持传参,整合C程序

    1,查看指定用户下的进程pid

  3. Java虚拟机类载入顺序

    Java虚拟机在载入类的时候.先初始化父类静态变量.再初始化子类静态变量.然后载入父类,最后载入子类 public class Parent { static{ System.out.println( ...

  4. 2014/4月金山WPS笔试

    今晚去參加了金山的笔试. 一開始还以为选C++的人不会非常多. 我去啊,一去到,好多人,一整个大教室都快满人了. 还好我算是去的比較早的了. 还拿到了一个位置. 金山还是挺不错的,对于我这类还没有实力 ...

  5. mongodb 学习笔记 04 -- 游标、索引

    游标 var cursor = db.collectionName.find() 创建游标 cursor.hasNext() 是否有下一个元素 cursor.next() 取出下一个元素 比如 whi ...

  6. 警惕:利用Dropbox链接散播的恶意软件

    趋势科技近期发现好几起利用热门文档代管服务Dropbox的垃圾邮件.邮件的内嵌链接会下载UPATRE恶意软件变种.UPATRE下面载恶意软件而恶名昭彰,当中包含ZBOT恶意软件.CryptoLocke ...

  7. VIM 中 查看{}是否闭合,按%跳转到下个闭合

    VIM 中 查看{}是否闭合,按%跳转到下个闭合

  8. DevExpress GridView使用技巧之列标题点击事件

    在这里使用GridView的MouseDown事件.这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上.GridHitInfo根据鼠标点击的x.y坐标获取该点的相关信息, ...

  9. zepto.js 处理Touch事件(实例)

    处理Touch事件能让你了解到用户的每一根手指的位置,在touch事件触发的时候产生,可以通过touch event handler的event对象取到,如果基于zepto.js开发,一般是通过eve ...

  10. VS单元测试入门实践教程

    摘要:本教程不会介绍单元测试的基本理论知识,也不会和大家讨论在实际项目中是否需要写单元测试代码的问题.但是如果你此时想使用VS中的单元测试的工具来测试某个方法是否正确,可你又从来没真正实践过,那么本教 ...