POJ——3061Subsequence(尺取法或二分查找)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11224 | Accepted: 4660 |
Description
Input
Output
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
尺取法做法:一直向前增加num[r],直到不能增加,再判断r-l,然后再一直向前减去num[l],然后判断是否小于s。一直循环。70+ms
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int N=100010;
int list[N];
inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
int main (void)
{
int t,n,s,i,j,temp,sum,ans;
scanf("%d",&t);
while (t--)
{
memset(list,0,sizeof(list));
scanf("%d%d",&n,&s);
sum=0;
for (i=1; i<=n; i++)
{
scanf("%d",&list[i]);
}
int l,r,dx;
l=r=1;
temp=0;
dx=N;
while (1)
{
while (r<=n&&temp<s)//r向前递增
{
temp+=list[r++];
}
if(temp<s)
break;
dx=min(r-l,dx);
temp-=list[l++];//l向前递增
}
if(dx==N)//特判
puts("0");
else
printf("%d\n",dx);
}
return 0;
}
二分查找做法:建立另外一个数组sufix来储存前缀和,然后题目变成了保证sufix[r]-sufix[l-1]>=S这样的条件下求r-l的最小值,显然r>=l。将这个式子移项得到sufix[r]>=S+sufix[l]。
就是说当遍历l的时候要使r最小,然后就用自带的lowerbound函数来求。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int N=100010;
int list[N];
int sufix[N];
int main (void)
{
ios::sync_with_stdio(false);
int t,n,s,i,j,temp,sum,ans;
cin>>t;
while (t--)
{
cin>>n>>s;
for (i=1; i<=n; i++)
{
cin>>list[i];
sufix[i]=sufix[i-1]+list[i];
}
int dx=N;
if(sufix[n]<s)//特判
{
cout<<0<<endl;
continue;
}
for (i=0; sufix[i]+s<=sufix[n]; i++)//由于是公式内为l-1,因此i要从0开始,即
{
int t=lower_bound(sufix+i+1,sufix+n+1,s+sufix[i])-(sufix+i);
dx=min(t,dx);
}
cout<<dx<<endl;
}
return 0;
}
POJ——3061Subsequence(尺取法或二分查找)的更多相关文章
- POJ:3061-Subsequence(尺取法模板详解)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
- poj 2100(尺取法)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 6107 Accepted: 1444 ...
- POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...
- POJ 1019:Number Sequence 二分查找
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36013 Accepted: 10409 ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
随机推荐
- php有哪些优化技巧
1. echo 比 print 快.2. 使用echo的多重参数代替字符串连接.3. 在执行for循环之前确定最大循环数,不要每循环一次都计算最大值,最好运用foreach代替.4. 对global变 ...
- 2018.4.10 Ubuntu cat命令解答、用法
cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: 1.一次显示整个文件. $ cat filename 2.从键盘创建一个文件. $ cat > ...
- python_86_shutil模块
#高级的文件.文件夹.压缩包.处理模块 import shutil f1=open('sys模块.py','r',encoding='utf-8') f2=open('copy1.py','w',en ...
- bootstrap 超大屏幕(Jumbotron)
本章将讲解Bootstrap的一个特性:超大屏幕(Jumbonron),顾名思义该组件可以增加标题的大小,并为登录页面的内容添加更多的外边矩. 使用超大屏幕的步骤如下: 1.创建一个还有class.j ...
- 快速排序和快速选择(quickSort and quickSelect)算法
排序算法:快速排序(quicksort)递归与非递归算法 TopK问题:快速选择(quickSelect)算法 import java.util.*; import java.lang.*; publ ...
- python入门:简单模拟登陆时UTF-8转换成GBK编码
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 给变量x赋值为字符串‘请输入用户名:’ 变量x_unicode的赋值等于 ...
- Vue实例和生命周期
创建一个Vue实例 每个Vue应用都是通过Vue函数创建一个新的Vue实例开始: var vm = new Vue({ //选项 }) 数据与方法 当一个Vue实例被创建时,它向Vue的响应式系统中加 ...
- paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals
1.the fsm coding style should be easily modifiable to change state encoding and FSM styles. FSM 的的 状 ...
- leepcode作业解析-5-21
25.Nim游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编 ...
- LightOj:1030-Discovering Gold(期望dp模板)
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...