题目链接:http://poj.org/problem?id=3061

题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s。

统计[1~i]的子序列和sum(i),(sum(0)=0)。然后求一个区间[i,j]的和即为sum(j)-sum(i-1) (i > 0)。

由于给定序列没有负数,因此sum是个严格不减的序列。

转换成一个求最大值最小的问题,可以二分枚举序列长度,在前缀和上计算子序列[i-1,i+m-1]的和。如果存在一个满足子序列和≥s的,则缩小序列长度并记下当前值,反之扩大。复杂度为O(nlgn)。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
int n, s;
int x[maxn];
int sum[maxn]; bool ok(int mm) {
for(int i = ; i <= n - mm + ; i++) {
// printf("%d %d\n", i-1, i+mm-1);
if(sum[i+mm-] - sum[i-] >= s) return ;
}
return ;
} int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
memset(sum, , sizeof(sum));
scanf("%d %d", &n, &s);
for(int i = ; i <= n; i++) {
scanf("%d", &x[i]);
sum[i] = sum[i-] + x[i];
}
if(sum[n] < s) {
printf("0\n");
continue;
}
int ans;
int ll = ;
int rr = n;
while(ll <= rr) {
int mm = (ll + rr) >> ;
if(ok(mm)) {
ans = mm;
rr = mm - ;
}
else ll = mm + ;
}
printf("%d\n", ans);
}
}

此题也可以用尺取法,维护两个指针从左到右扫描所存序列,复杂度为O(n)。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
int n, s;
int x[maxn]; int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &s);
int sum = ;
for(int i = ; i <= n; i++) {
scanf("%d", &x[i]);
sum += x[i];
}
if(sum < s) {
printf("0\n");
continue;
}
int ans = 0x7f7f7f;
int ll = ;
int rr = ;
sum = ;
while() {
while(rr <= n && sum <= s) {
sum += x[rr++];
}
if(sum < s) break;
ans = min(ans, rr-ll);
sum -= x[ll++];
}
printf("%d\n", ans);
}
}

[POJ3061]Subsequence(二分,前缀和)的更多相关文章

  1. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

  2. POJ3061 Subsequence(二进制前缀和法律+仿真足)

    二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度 ...

  3. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

  4. poj 3061 Subsequence 二分 前缀和 双指针

    地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...

  5. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  6. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  7. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  8. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  9. 【BZOJ-1926】粟粟的书架 二分 + 前缀和 + 主席树

    1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Sec  Memory Limit: 552 MBSubmit: 616  Solved: 238[Submit][Statu ...

随机推荐

  1. 2014_acmicpc_shanghai_google

    I http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84975#problem/I 题意:我方有n个士兵,敌方有m个士兵,每个士兵有攻击力和 ...

  2. iTunes Connect TERMS OF SERVICE

    iTunes Connect TERMS OF SERVICE THESE TERMS OF SERVICE CONSTITUTE A LEGAL AGREEMENT BETWEEN YOU AND ...

  3. Winform 打印PDF顺序混乱,获取打印队列

    工作中PDF打印顺序混乱着实让我疼痛了好久,其实决绝方法非常简单,但没有想到这个点子的时候确实让我走了很多弯路 这里文章写出来并不是为了炫耀什么,只是觉得发现些好东西就分享出来而已,同时也做个记录,方 ...

  4. GameMap地图初始化

    init_map(res_path) .初始化mapbase的基本信息 pos2d screen_area = {, }; //普通屏幕大小 m_spBase->init(screen_area ...

  5. js中的null VS undefined

    var a;------>undefined. JS变量的默认值.注意点在于判断变量的值为null.这是错误的.比如 if( a === null ) { // TODO; }; 实际上是und ...

  6. HDOJ 3183 A Magic Lamp

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. 山寨小小军团开发笔记 之 GamePool

    很多时候我们对于物体(比如弓箭)大量的生成与销毁,这个时候可以把弓箭放在内存池中进行管理,加快体验.自己Copy了一个简易版的. 一.代码 GameObjectPoolManager.cs using ...

  8. Ogre1.8.1 Basic Tutorial 6 - The Ogre Startup Sequence

    原文地址:http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Basic+Tutorial+6&structure=Tutorials 1. ...

  9. 几种CPU架构

    原文链接:http://blog.csdn.net/wyzxg/article/details/5027738 这几天在下载RPM包的时候,总会看见x86,x86-64,IA64,i386,i586等 ...

  10. jquey ajax 无刷新提交form

    http://bbs.csdn.net/topics/380237868 $.ajax({ type: "POST", url:ajaxCallUrl, data:$('#your ...