博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Subsequence(暴力+二分)
阅读量:4469 次
发布时间:2019-06-08

本文共 3094 字,大约阅读时间需要 10 分钟。

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

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23 题解:让求连续的一个序列数之和大于等于S的最短序列长度;这道题,我前后换了三种方法才A了,刚开始看到,一想不就是个线段树,写完了发现答案不对。。。然后发现线段树只能找到一半, 还呆加上区间合并,区间合并也很可能不对,然后想着树状数组,写了一半感觉还不如用个数组直接存到i的总值和,然后找到起点终点就好了,于是开始了暴力,暴力肯定超时啊;就想着二分下; 调试了下就过了;二分还要判断下当前点与前一个点插哪个; 可能我写的太麻烦了。。。有空看看大神怎么写的; AC代码:
#include
#include
#include
#include
#include
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,rconst int INF=0x3f3f3f3f;const int MAXN=100010;int tree[MAXN];int main(){ int T,N,M; SI(T); while(T--){ SI(N);SI(M); mem(tree,0); int ans=INF; int t=1; for(int i=0;i
=0;i--){ if(tree[i]-M>=0){ 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+1); //printf("%d\n",ans); } } if(ans==INF)puts("0"); else printf("%d\n",ans); } return 0;}

 

// handsomecui.cpp : 定义控制台应用程序的入口点。////#include "stdafx.h"#include 
#include
#include
#include
using namespace std;typedef long long LL;const int MAXN = 100010;LL seq[MAXN];int erfen(int l, int r, int v){ int mid; while(l <= r){ mid = (l + r) >> 1; if(seq[mid] >= v) r = mid - 1; else l = mid + 1; } return r + 1;}int main(){ int N, S, T; cin >> T; while(T--){ cin >> N >> S; int ans = 0x3f3f3f3f; memset(seq, 0, sizeof(seq)); for(int i = 0; i < N; i++){ cin >> seq[i]; if(i) seq[i] += seq[i - 1]; } for(int i = 0; i < N; i++){ if(seq[i] - S < 0) continue; int p = erfen(0, i - 1, 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("0"); else printf("%d\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/handsomecui/p/5244644.html

你可能感兴趣的文章
正则表达式
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
vue实战教程
查看>>
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
Windows Store App之数据存储
查看>>
English class 82 The Importance of traveling
查看>>
python用递归函数解汉诺塔游戏
查看>>
Redis与Python交互
查看>>
Maximum-SubsequenceSum
查看>>
常用的一些shell变量
查看>>
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
Shell编程-条件测试 | 基础篇
查看>>
AngularJs学习笔记1——总体介绍
查看>>
C语言第十二讲,文件操作.
查看>>
绝对定位和相对定位
查看>>
实习第二天——学习mac终端命令(unix命令)和git代码管理
查看>>
微信支付
查看>>