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 ...
随机推荐
- OpenCV数字识别
输入命令: conda install opencv 返回信息:
- Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...
- Windows篇:链接linux->"Xmanager"
Windows篇:链接linux->"Xmanager" Xmanager是什么? Xmanager是一款小巧.便捷的浏览远端X窗口系统的工具.在工作中经常使用Xmanage ...
- 5.3 Nginx 动静分离
Server 脚本片段 server { listen ; server_name ccserver1; #charset koi8-r; access_log logs/host.access.lo ...
- 【LOJ2540】「PKUWC2018」随机算法
题意 题面 给一个 \(n\) 个点 \(m\) 条边的无向图.考虑如下求独立集的随机算法:随机一个排列并按顺序加点.如果当前点能加入独立集就加入,否则不加入.求该算法能求出最大独立集的概率. \(n ...
- Vue简介-MVVM是什么?
Vue.js - Day1 课程介绍 前5天: 都在学习Vue基本的语法和概念:打包工具 Webpack , Gulp 后5天: 以项目驱动教学: 什么是Vue.js Vue.js 是目前最火的一个前 ...
- OO第四次博客作业(第四单元作业及期末总结)
(注意:本文写作顺序与作业要求不完全一致,但涵盖了作业的所有要求) 一学期的BUAA特色OO课程结束了. PART 1 我想先写我这一学期的感想 从第一单元满怀期待地写完多项式求值到最后看着60分不 ...
- NO33 第6--7关题目讲解
客户端(电脑)通过浏览器输入域名,先找hosts文件及本地dns缓存,若都没有,就找localDNS服务器,若没有,localDNF服务器找根服务器(全球13台的那个根”.“服务器),根就把.com这 ...
- HTML学习第七天(一)
HTML学习第七天(一) aside元素用来表示当前或文章的附属信息部分,它可以包含与当前页面或主要内容的相关引用.侧边栏.广告.导航条,以及其他类似的有区别于主要内容的部分 <!DOCTYPE ...
- C语言中语句的跨行支持总结
C语言中语句的跨行支持总结: 预处理一行写不下: 把一个预处理指示写成多行要用""续行,因为根据定义,一条预处理指示只能由一个逻辑代码行组成. 正常程序一行写不下: 把C代码写成多 ...