POJ 3061 Subsequence 尺取法 挑战146页
---恢复内容开始---
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10487 | Accepted: 4337 |
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
Source
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
long long mid,l,r,n,m;
int sum[100005];
int main()
{
int n,cas,s,k,ans,res;
cin>>cas;
while(cas--)
{
scanf("%d %d",&n,&s);
sum[0]=0;ans=100005;
for(int i=1;i<=n;i++)
{
scanf("%d",&sum[i]);
sum[i]+=sum[i-1]; //将数组求和是常用的技巧
}
if(sum[n]<s)
{
cout<<"0"<<endl;
continue;
}
for(k=0;sum[n]-sum[k]>=s;k++) //枚举起点
{
res=lower_bound(sum+k,sum+n+1,sum[k]+s)-(sum+k); //第一个大于等于该值的位 //置
if(res<ans)
ans=res;
}
printf("%d\n",ans);
}
return 0;
}
下面重点介绍尺取法:
尺取法的核心思想:假设当前a[s]+s[s+1]+...a[t]是最初>=sum的,那么当s变为s+1时,即a[s+1]+s[s+2]+...a[t+n]要想仍然成为最初>=sum的,则t+n>=t(a[s]可能为0),依据这一思想,可以设置两个”指针“,一个指向p一连续序列的开头位置,另一头q指向结尾位置,该子序列之和>=sum,则当p向右推移时,则q也应向右推移直到两指针之间的序列之和再次最初>=sum,复杂度是O(n),其实觉得尺取法最显著的优势就在于能够保存中间一部分子序列的计算结果,这也是其复杂度更低的原因。
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
long long mid,l,r,n,m;
int a[100005];
int main()
{
int n,cas,s;
cin>>cas;
while(cas--)
{
scanf("%d %d",&n,&s);
a[0]=0;a[n+1]=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int p=0,q=0,v=0,ans=n+1;
while(v<s&&q<=n)
{
q++;
v+=a[q];
}
if(q==n+1)
{
cout<<"0"<<endl;
continue;
}
for(;;)
{
while(v<s&&q<=n)
{
q++;
v+=a[q];
}
if(q==n+1)
break;
if(ans>q-p)
ans=q-p;
p++;
v-=a[p];
}
printf("%d\n",ans);
}
return 0;
POJ 3061 Subsequence 尺取法 挑战146页的更多相关文章
- POJ 3061 Subsequence(尺取法)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18145 Accepted: 7751 Desc ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
- POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9050 Accepted: 3604 Descr ...
- poj 3061 题解(尺取法|二分
题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...
- POJ 3061 Subsequence 尺取
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14698 Accepted: 6205 Desc ...
- POJ 3061 Subsequence 二分或者尺取法
http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...
- POJ 3061 Subsequence ( 尺取法)
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence【二分答案】||【尺取法】
<题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...
随机推荐
- ~艾比郎~学Python之Python基础
第一步: 尝试写Hello World程序 这是我用Python写出的第一个程序,我的一小步,我人生的一大步 在linux 下创建一个文件叫hello.py,如图所示 然后执行命令:python he ...
- PLSQL中查到的数据和程序中查询到的不一样
1.首先看下你的修改或者新增的SQL是否提交.
- CentOS7通过YUM安装NGINX稳定版本
创建 nginx.repo 文件 $ cd /etc/repos.d/ $ vim nginx.repo #写入以下内容 [nginx-stable] name=nginx stable repo b ...
- Oracle sqlplus prelim 参数介绍
SQL>conn / as sysdba ORA-00020: maximum number of processes (xxxx) exceeded 报错解决方法 解决 ORA-00020 错 ...
- Bat批处理文件入门
这个东西吧,感觉在使用windows时作用不是很大,毕竟在windows环境下命令行确实用的比较少,但有时候也会用到,比如测试算法程序时使用批处理+文件可以省去每次手动输入.赶巧最近对批处理也比较感兴 ...
- 使用shell脚本自动打包上传 fir.im
http://blog.csdn.net/wang631106979/article/details/52299083
- DedeAMPZ配置php的curl扩展
DedeAMPZ配置php的curl扩展 curl是一个利用URL语法在命令行方式下工作的文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, ...
- 5.flask与数据库
1.安装postgresql 注意:在flask中,操作数据库还是通过orm调用驱动来操作.sqlalchemy是python下的一款工业级的orm,比Django自带的orm要强大很多,至于什么类型 ...
- 【有钱的大佬看过来】Java开发学习大纲
Java开发学习大纲文档V7.0 有钱的大佬可以买下这个版权,全网最完整最详细了,没钱的大佬可以按照自己的方式去整理.有需要的私聊作者QQ:253173641 来源于-幸福的沉淀:https://ww ...
- 六、取消eslint 校验代码
一.取消eslint 校验代码 删除 "eslintConfig": { "root": true, "env": { "node ...