poj 3061 Subsequence (尺取法)
来源:互联网 发布:杨千嬅 唱功 知乎 编辑:程序博客网 时间:2024/05/17 01:39
题目链接:http://poj.org/problem?id=3061
题目大意:给出长度为n的数列,以及一个整数S,求总会和不小于S的连续子序列的最小长度。如果不存在解,则输出0。
分析:二分法可解,但是时间复杂度为O(nlgn),优化算法后,时间复杂度为O(n)。
ac代码
#include<cstdio>#define M 100010int a[M];int min(int x, int y){ if(x < y)return x; else return y;}int main(){ int T; scanf("%d", &T); for(int t = 1; t <= T; t++) { int N, S; scanf("%d %d", &N, &S); for(int i = 0; i < N; i++) scanf("%d", &a[i]); int res = N + 1; int sum = 0, ans = 0, s = 0; while(true) { while(ans < N && sum < S) sum = sum + a[ans++]; if(sum < S)break; res = min(res, ans - s); sum = sum - a[s++]; } if(res > N)res = 0; printf("%d\n", res); } return 0;}
阅读全文
0 0
- poj 3061--Subsequence(尺取法)
- poj 3061 Subsequence(尺取法)
- POJ 3061 Subsequence ( 尺取法 )
- POJ 3061 Subsequence(尺取法)
- Poj 3061 Subsequence【尺取法】
- poj 3061 Subsequence (尺取法)
- POJ 3061 Subsequence【尺取法】
- POJ 3061 Subsequence【尺取法】
- 尺取法 POJ 3061 Subsequence
- 【尺取法】poj 3061 Subsequence
- [尺取法] poj 3061 Subsequence
- poj 3061 Subsequence尺取法
- POJ 3061 Subsequence 尺取法
- POJ 3061 Subsequence 【尺取法】
- Poj 3061 Subsequence【尺取法】
- 【poj】 3061 Subsequence 尺取法
- POJ 3061 Subsequence 尺取法
- POJ 3061 - Subsequence(尺取法)
- Set集合
- 什么是redis,redis能做什么,redis的应用场景
- pandas的部分用法
- Cannot resolve module 'fs'
- tensorflow--tf.one_hot()函数示例
- poj 3061 Subsequence (尺取法)
- 【vue 组件 mint-ui】 看了一下源码,给轮播图Swiper封装自定义跳转的函数
- java泛型
- break和continue的区别和作用?
- C++中重载、重写、重定义
- java 多线程随记
- SwipyRefreshLayout+recyclerview
- TCP和UDP的最完整的区别
- repo的介绍