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. 观察者模式模拟YUI事件机制

    首先介绍下YUI的事件机制,很好理解,因为和浏览器事件机制差不多.看懂下面几个方法就可以了: publish: 创建自定义事件.第一个参数是事件类型,第二个参数是一个对象,里面可以设置默认动作 on: ...

  2. SharedPreference对象及其xml文件

    SharedPreferences对象----->getXXX SharedPreferences.Editor对象---->putXXX

  3. Unix/Linux环境C编程入门教程(26) 字符数字那些事儿

    1.gcvt() strtod() strtol() strtoul() toascii() tolower() toupper函数介绍 gcvt(将浮点型数转换为字符串,取四舍五入) 相关函数 ec ...

  4. CDOJ 631 敢说敢做 记忆化搜索and动规

    //跟沈爷学的 传送门http://www.cnblogs.com/Xiper/p/4639636.html #include<cstdio> #include<iostream&g ...

  5. DB操作用法总结。

    用到了慢慢总结.用到了随时更新. 其实可以看手册了.但是看了完了手册之后,还是记不住. 1. mysql select * from table where id in(1,2,3,3,4) 怎么能显 ...

  6. Unity doesn't load, no Launcher, no Dash appears

    1. 重新安装 ubuntu-desktop不起作用. Enter the following commands:- Ctrl+Alt+F1 login there by user name and ...

  7. STL入门

    STL入门 STL的组成 六大组件 容器container 算法algorithm 迭代器iterator 仿函数function object 适配器adaptors 空间配制器allocator ...

  8. A Game with Colored Balls

    题目链接 题意: 给一个长度为n的字符串,每次删除字母同样切连续的串,假设有多个,删除最左边的.最长的串.每次删除输出串的字母,每一个字母的下标(1-n) N (1 ≤ N ≤ 106),串仅仅包含r ...

  9. SNMP协议具体解释

    简单网络管理协议(SNMP)是TCP/IP协议簇的一个应用层协议.在1988年被制定,并被Internet体系结构委员会(IAB)採纳作为一个短期的网络管理解决方式:因为SNMP的简单性,在Inter ...

  10. 从U盘安装win8系统

    http://blog.csdn.net/pipisorry/article/details/40662397 lz提示,下面也能够用于win7.linux等操作系统的安装 一.下载windows安装 ...