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 ...
随机推荐
- JS数据统计表 highcharts.js的运用
参考地址 http://www.runoob.com/highcharts/highcharts-column-basic.html 1.下载JS文件引入,或者用CDN function getCou ...
- AJAX的表单请求POST请求方式
表单数据的提交 action : 数据提交的地址,默认是当前页面 method : 数据提交的方式,默认是get方式 post: 把数据名称和数据值用=连接,如果有多个的话,那么他会把多个数据组合用& ...
- 使用Linux命令修改数据库密码
通过登录mysql系统,# mysql -uroot -pEnter password: [输入原来的密码]mysql>use mysql;mysql> update user set p ...
- 输入、输出(iostream)
在一个程序当中输入和输出都扮演着重要的角色,所以掌握基本输入输出是入门一门语言所必不可少的.本文主要简单叙述java的输入和输出. package ios; import java.util.Scan ...
- java字符集编码乱码问题
博客分类: web javajspservlet 最近做网页这块时碰到了正文字符乱码问题.别看这小小的一个问题,对我来说却花费了好长一段时间.现在让我慢慢分析它吧(说实话.这些有部分是从网上找的,但 ...
- javascript if else优化指南
不管是平时在学习js中还是在项目书中写js代码,都避免不了一个问题就是有时候要做大量的分支判断,很多人的第一反应就是使用if else.无可厚非,if else早平时做分支判断的时候是非常好用的,但是 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:清除浮动1
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 猎鹰9火箭(Falcon 9)
翻译自:https://www.spacex.com/falcon9 使用了有道词典的翻译,以及本人的修正. 猎鹰9号是SpaceX公司设计和制造的两级火箭,用于可靠和安全地将卫星和“龙”号宇宙飞船送 ...
- vue-router 一个十分简单的应用场景
时间:2018-03-28 关于vue-router: 这里只大致说一下构建过程和使用情况,将就看看!! 我使用的是vue-cli脚手架+webpack构建的项目 安装vue-cli脚手架 npm i ...
- python的super()以及父类继承
Python中子类调用父类的方法有两种方法能够实现:调用父类构造方法,或者使用super函数(两者不要混用). 使用“super”时经常会出现代码“super(FooChild,self).__ini ...