Subsequence——二分法
来源:互联网 发布:数据库系统概论视频 编辑:程序博客网 时间:2024/05/17 09:23
题目
Subsequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16652 Accepted: 7073
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
Source
Southeastern Europe 2006
心得:
数组sum[i]表示a[i]的前i-1项和,sum[0]=0,(i,R]内相邻数的和即为sum[R]-sum[i]。
以
5 11
1 2 3 4 5 中i=0 为例,显示二分法过程 L=0 m=2 R=5
| | |
m=(R+L)/2; m=2; sum[m]-sum[i]<S;L=m;
L=2 m=3 R=5
| | |
m=(R+L)/2; m=3; sum[m]-sum[i]<S;L=m;
L=3 m=4 R=5
| | |
此时R-L=1;不符合条件,退出循环,R=5,长度为R-i=5;
代码<c语言>
#include<stdio.h>
int a[100005],sum[100005];
int find(int L,int R,int S){//范围(L,R],S用来比较的数,二分法查找最先大于等于S的数
int m,i;
i=L;
sum[0]=0;
while(R-L>=2){
m=(R+L)/2;
if(sum[m]-sum[i]<S)//(i,m]
L=m;
else
R=m;
}
return R;
}
int main(){
int i,j,n,N,S,R,min;
scanf("%d",&n);//测试的次数
while(n--){
scanf("%d%d",&N,&S);//N个整数,S是要大于等于的数
min=N+1;
for(i=0;i<N;i++){
scanf("%d",&a[i]);
sum[i+1]=sum[i]+a[i];//sum[i]记录前i-1项和,令sum[0]=0
}
if(sum[N]<S){//不会大于等于S,反之,一定存在大于等于S
printf("%d\n",0);
continue;
int a[100005],sum[100005];
int find(int L,int R,int S){//范围(L,R],S用来比较的数,二分法查找最先大于等于S的数
int m,i;
i=L;
sum[0]=0;
while(R-L>=2){
m=(R+L)/2;
if(sum[m]-sum[i]<S)//(i,m]
L=m;
else
R=m;
}
return R;
}
int main(){
int i,j,n,N,S,R,min;
scanf("%d",&n);//测试的次数
while(n--){
scanf("%d%d",&N,&S);//N个整数,S是要大于等于的数
min=N+1;
for(i=0;i<N;i++){
scanf("%d",&a[i]);
sum[i+1]=sum[i]+a[i];//sum[i]记录前i-1项和,令sum[0]=0
}
if(sum[N]<S){//不会大于等于S,反之,一定存在大于等于S
printf("%d\n",0);
continue;
}//continue退出当前循环,开始下一次循环
for(i=0;i<N&&sum[N]-sum[i]>=S;i++){//如果不满足这个条件sum[N]-sum[i]>=S,就没必要查找
R=find(i,N,S);//调用这个函数,在区间(i,N]内用二分法查找最先大于等于S的sum[R]-sum[i]
min=min<(R-i)?min:(R-i);//R-i为区间的距离,即大于等于S的相邻数的个数
}
printf("%d\n",min);
}
return 0;
}
for(i=0;i<N&&sum[N]-sum[i]>=S;i++){//如果不满足这个条件sum[N]-sum[i]>=S,就没必要查找
R=find(i,N,S);//调用这个函数,在区间(i,N]内用二分法查找最先大于等于S的sum[R]-sum[i]
min=min<(R-i)?min:(R-i);//R-i为区间的距离,即大于等于S的相邻数的个数
}
printf("%d\n",min);
}
return 0;
}
AC情况:
阅读全文
1 0
- Subsequence——二分法
- 二分法subsequence
- 二分法 Subsequence
- Subsequence --二分法
- Subsequence(二分法)
- 【POJ 3061】Subsequence(二分法)
- ACM投票——二分法
- 数据结构——二分法查找
- 数据结构——二分法查找
- 二分法查找——对数
- 二分法——while等号
- 简单递归—二分法查找
- 排序—二分法插入排序
- HDU1159——Common Subsequence
- POJ1458——Common Subsequence
- ZOJ3349——Special Subsequence
- 北大ACM3061——Subsequence
- POJ——3061 Subsequence
- Java核心技術卷_6_控制流程
- luogu p1122 DP
- 什么是DOM?
- 聊聊 CountDownTimer 使用过程中的注意事项
- 改善用户体验,web前段性能优化
- Subsequence——二分法
- 函数返回指向内存的指针
- 内部类
- MySQL
- 【Leetcode】【python】Multiply Strings
- 提交更新代码到github(简单个人项目)
- RESTful的理解
- 带负号字符串转换成整型
- HDU 4507 吉哥系列故事——恨7不成妻[数位dp]