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. Android_listview设置每条信息的间距

    Android_listview设置每条信息的间距 设置listView的item间距,可以在xml布局文件中的listView下设置xml属性: android:divider="#000 ...

  2. 实用的PHP正则表达式

    正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本文为你介绍10种常见的实用PHP正则表达 ...

  3. IP地址分类与识别错误

    //描述:  请解析IP地址和对应的掩码,进行分类识别.要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类. //所有的IP地址划分为 A,B,C,D,E五类 //A类地址1.0.0.0 ...

  4. 【具体数学 读书笔记】1.2 Lines in the Plane

    本节介绍平面划分问题,即n条直线最多把一个平面划分为几个区域(region). 问题描述: "What is the maximum number Ln of regions defined ...

  5. 构造函时和this指针

    通常this指针在对象构造完毕后才完全生成,而在构造函数执行过程中,对象还没有完全生成,所以this指针也是没有完全生成的,在构造函数中使用this指针会存在问题,应该尽量避免. 构造函数中可以访问对 ...

  6. systemctl 命令完全指南

    http://www.linuxidc.com/Linux/2015-07/120833.htm Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器. System ...

  7. Jam's math problem(思维)

    Jam's math problem Submit Status Practice HDU 5615   Description Jam has a math problem. He just lea ...

  8. [caffe]深度学习之图像分类模型AlexNet解读

    在imagenet上的图像分类challenge上Alex提出的alexnet网络结构模型赢得了2012届的冠军.要研究CNN类型DL网络模型在图像分类上的应用,就逃不开研究alexnet.这是CNN ...

  9. OpenCV——PS 图层混合算法(一)

    详细的算法原理能够參考 PS图层混合算法之中的一个(不透明度,正片叠底,颜色加深,颜色减淡) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #de ...

  10. Banner 切换

    在线项目 :  Banner 切换 时间 : 2个小时 (15:00 - 17:00)满分 : 100分------------------------------------------------ ...