ZOJ - 3123 Subsequence (滑动窗口)
题意:给定N个数,求和大于等于S的最短连续子序列的长度。
分析:滑动窗口即可。两种写法。
1、
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int main(){
int T;
scanf("%d", &T);
while(T--){
int N, S;
scanf("%d%d", &N, &S);
int tot = 0;
for(int i = 0; i < N; ++i){
scanf("%d", &a[i]);
tot += a[i];
}
if(tot < S){
printf("0\n");
continue;
}
int st = 0, et = 0;
int sum = 0;
int cnt = 0x7f7f7f7f;
while(et < N){
while(et < N && sum + a[et] < S){
sum += a[et];
++et;
}
cnt = min(cnt, et - st + 1);
sum -= a[st];
++st;
}
printf("%d\n", cnt);
}
return 0;
}
2、
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int main(){
int T;
scanf("%d", &T);
while(T--){
int N, S;
scanf("%d%d", &N, &S);
int tot = 0;
for(int i = 0; i < N; ++i){
scanf("%d", &a[i]);
tot += a[i];
}
if(tot < S){
printf("0\n");
continue;
}
int st = 0, et = 0;
int sum = 0;
int cnt = 0x7f7f7f7f;
for(int i = 0; i < N; ++i){
sum += a[i];
while(sum - a[st] >= S){
sum -= a[st];
++st;
}
if(sum >= S){
cnt = min(cnt, i - st + 1);
sum -= a[st];
++st;
}
}
printf("%d\n", cnt);
}
return 0;
}
ZOJ - 3123 Subsequence (滑动窗口)的更多相关文章
- [LeetCode] Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- hdu-5497 Inversion(滑动窗口+树状数组)
题目链接: Inversion Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- [Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口
[题目] Given a string, find the length of the longest substring without repeating characters. [举例] Exa ...
- [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- TCP/IP 协议中的滑动窗口
一个例子明白发送缓冲区.接受缓冲区.滑动窗口协议之间的关系. 在上面的几篇文章中简单介绍了上述几个概念在TCP网络编程中的关系,也对应了几个基本socket系统调用的几个行为,这里再列举一个例子,由于 ...
- Storm Windowing storm滑动窗口简介
Storm Windowing 简介 Storm可同时处理窗口内的所有tuple.窗口可以从时间或数量上来划分,由如下两个因素决定: 窗口的长度,可以是时间间隔或Tuple数量: 滑动间隔(slidi ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
- TCP 三次握手四次挥手, ack 报文的大小.tcp和udp的不同之处、tcp如何保证可靠的、tcp滑动窗口解释
一.TCP三次握手和四次挥手,ACK报文的大小 首先连接需要三次握手,释放连接需要四次挥手 然后看一下连接的具体请求: [注意]中断连接端可以是Client端,也可以是Server端. [注意] 在T ...
- tcp协议头窗口,滑动窗口,流控制,拥塞控制关系
参考文章 TCP 的那些事儿(下) http://coolshell.cn/articles/11609.html tcp/ip详解--拥塞控制 & 慢启动 快恢复 拥塞避免 http://b ...
随机推荐
- 新闻网大数据实时分析可视化系统项目——13、Cloudera HUE大数据可视化分析
1.Hue 概述及版本下载 1)概述 Hue是一个开源的Apache Hadoop UI系统,最早是由Cloudera Desktop演化而来,由Cloudera贡献给开源社区,它是基于Python ...
- MAC Matlab 中文乱码
环境:macOS High Sierra 10.13.4 问题:文件中文注释乱码(再次打开文件时) / 控制台输出中文乱码 解决方法: 官网下载补丁(https://ww2.mathworks.cn/ ...
- SQL中limit的用法
limit子句用于限制查询结果返回的数量 格式: select * from tableName limit i,n # tableName:表名 # i:为查询结果的索引值(默认从0开始),当i=0 ...
- CH8 课后习题
8.1和8.2 #include <iostream> using namespace std; istream& f(istream& in) { int v; in & ...
- Vim学习1移动光标
vim adventure上面做了大量练习,是个好软件只是需要收费 HJKLWBE 首先HJKL分别是移动上下左右的 H:光标左移 J:下移 K:上移 L:右移 W:跳转到下一个单词的第一个字,注意是 ...
- intel关于spark gc的优化建议
Apache Spark由于其出色的性能.简单的接口和丰富的分析和计算库而获得了广泛的行业应用.与大数据生态系统中的许多项目一样,Spark在Java虚拟机(JVM)上运行.因为Spark可以在内存中 ...
- java 根据传入的时间获取当前月的第一天的0点0分0秒和最后一天的23点59分59秒
/** * 获取指定日期所在月份开始的时间 * lkeji * @return */ public static String getMonthBegin(String specifiedDay) { ...
- Java入门基础(类的方法)
方法 1.添加方法 方法表示一个类能做什么.在Java里,方法和属性属于对等的术语,在一个类中,不仅可以添加属性,还可以添加方法. 类 { 属性:描述“我有什么” 方法:描述“我能做什么” } 例子: ...
- docker学习笔记-02:docker常用命令
一.帮助命令: 1.查看版本:docker version 2.查看信息:docker info 3.查看帮助信息:docker --help 二.镜像命令: (一).查看已有镜像: 1.命令:doc ...
- Java笔记: 初始化块
Java语言提供了很多类初始化的方法,包括构造器.初始化器等.除了这两种方法之外,我们还可以用初始化块(initialization block)来实现初始化功能. 基本语法 初始化块是出现在类声明中 ...