博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 3061 Subsequence(二分+前缀和)
阅读量:5265 次
发布时间:2019-06-14

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

Subsequence

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12333 Accepted: 5178
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
Source
Southeastern Europe 2006

/*题目大意:要求n个数总和不小于s的连续子序列的长度的最小值.二分+前缀和查询. */#include
#include
#define MAXN 100001using namespace std;int n,m,s[MAXN],sum[MAXN];bool check(int x){ for(int i=1;i<=n-x+1;i++){ if(sum[x+i-1]-sum[i-1]>=m) return true; } return false;}int erfen(int l,int r){ int mid,ans=0; while(l<=r){ int mid=(l+r)>>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } return ans;}int main(){ int t;scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&s[i]),sum[i]=s[i]+sum[i-1]; printf("%d\n",erfen(0,n+1)); } return 0;}

转载于:https://www.cnblogs.com/nancheng58/p/6070780.html

你可能感兴趣的文章
Android 分包 MultiDex 策略总结
查看>>
探寻 JavaScript 精度问题
查看>>
java配置定时任务的几种配置方式及示例
查看>>
WINDOWS xp用户账户怎么没了怎么办?
查看>>
区分多态和重载的区别
查看>>
抓取天猫手机评论
查看>>
模板设计模式的应用
查看>>
【井字游戏】做一款回忆童年的游戏
查看>>
testNG中@Factory详解
查看>>
[模拟赛] StopAllSounds
查看>>
高性能的异步爬虫
查看>>
数据结构(二):栈
查看>>
实训第五天
查看>>
平台维护流程
查看>>
SQL (FMDB)
查看>>
2012暑期川西旅游之总结
查看>>
Linux发行版的排行
查看>>
宾得镜头大全与发展史
查看>>
spread+wackamole打造全新高可用+负载均衡
查看>>
sql语句中的left join,right join,inner join的区别
查看>>