POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9050 | Accepted: 3604 |
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
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff; //无限大
int a[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,s;
cin>>n>>s;
for(int i=;i<n;i++)
{
cin>>a[i];
}
ll sum=a[];
int st=,en=;
int minn=inf;
while(en!=n&&st<=en)
{
//cout<<st<<" "<<en<<" "<<sum<<endl;
if(sum>=s)
{
minn=min(minn,en-st+);
sum-=a[st];
st++;
}
if(sum<s)
{
en++;
sum+=a[en];
}
}
if(minn==inf)
cout<<""<<endl;
else
cout<<minn<<endl;
}
return ;
}
POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法的更多相关文章
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
- 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(尺取法)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18145 Accepted: 7751 Desc ...
- 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(前缀+二分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的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...
- POJ 3061 Subsequence ( 尺取法)
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...
随机推荐
- python基础===一行 Python 代码实现并行(转)
原文:https://medium.com/building-things-on-the-internet/40e9b2b36148 译文:https://segmentfault.com/a/119 ...
- LinuxUSB驱动程序调试--009:编写应用程序---验证协议【转】
转自:http://biancheng.dnbcw.info/linux/257411.html [1] 如何编译X86下的 uBuntu APP---非常简单: gcc -o ...
- aarch64_m1
MAKEDEV-3.24-18.fc26.aarch64.rpm 2017-02-14 08:46 99K fedora Mirroring Project MUMPS-5.0.2-8.fc26.aa ...
- Vue 进阶教程之:详解 v-model
分享 Vue 官网教程上关于 v-model 的讲解不是十分的详细,写这篇文章的目的就是详细的剖析一下, 并介绍 Vue 2.2 v-model改进的地方,然后穿插的再说点 Vue 的小知识. 在 V ...
- CF3A 【Shortest path of the king】
一句话题意:在8 * 8的棋盘上,输出用最少步数从起点走到终点的方案 数据很小,可以广搜无脑解决 定义数据结构体 struct pos{ int x,y,s; //x.y表示横纵坐标,s表示步数 ]; ...
- 20165203《Java程序设计》第七周Java学习总结
20165203<Java程序设计>第七周Java学习总结 教材学习内容总结 第11章 JDBC与MySQL数据库 MySQL数据库管理系统 MySQL数据库管理系统,简称MySQL,是世 ...
- getch与getchar区别
getch(): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上 getchar(): 所在头文件:stdio.h getch与getchar基本功能相同,差别是getc ...
- .net程序员写业务代码需要注意的地方
代码规范要求1.命名空间规范:dao层的impl实现和接口采用一样的命名空间,到对应文件夹层:IxxDaoContext与其实现类采用顶级命名空间. 2.TableEntity文件夹:所有的实体放到各 ...
- Vue.js中 watch(深度监听)的最易懂的解释[转]
https://blog.csdn.net/qq_36688143/article/details/81287535 taskData: { handler(v) { // watch 方法其实默认写 ...
- thinkphp中常用的模板变量
在thinkphp中的模板要加载静态文件如css,js等文件时要经常用到模板常量. 假如项目放在/web/shop中,则如下所示对应常量的输出值: 1 2 3 4 5 6 7 8 9 // 不含域名 ...