有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度。

用数组b[i]存放序列的前i项和,所以b[i]是递增的。

遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S的最大的i,时间复杂度为O(nlongn)。

这里二分查找用到库函数lower_bound()

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[maxn], b[maxn]; int main(void)
{
#ifdef LOCAL
freopen("2678in.txt", "r", stdin);
#endif int n, S;
while(scanf("%d%d", &n, &S) == )
{
for(int i = ; i <= n; ++i)
{
scanf("%d", &a[i]);
b[i] = b[i-] + a[i];
}
int ans = n + ;
for(int j = ; j <= n; ++j)
{
int i = lower_bound(b, b+j, b[j]-S) - b;
if(i > )
ans = min(ans, j-i+);
}
printf("%d\n", ans == n+ ? : ans);
}
return ;
}

代码君一

继续优化:

由于j是递增的,bj也是递增的,所以bi-1≤bj-S的右边也是递增的。换句话说满足条件的i的位置也是递增的。

虽然有两层循环,但是i的值只增不减,所以时间复杂度为O(n)

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[maxn], b[maxn]; int main(void)
{
#ifdef LOCAL
freopen("2678in.txt", "r", stdin);
#endif int n, S;
while(scanf("%d%d", &n, &S) == )
{
for(int i = ; i <= n; ++i)
{
scanf("%d", &a[i]);
b[i] = b[i-] + a[i];
}
int ans = n + ;
int i = ;
for(int j = ; j <= n; ++j)
{
if(b[i-] > b[j] - S)
continue;
while(b[i] <= b[j] - S)
++i;
ans = min(ans, j-i+);
}
printf("%d\n", ans == n+ ? : ans);
}
return ;
}

代码君二

LA 2678 Subsequence的更多相关文章

  1. LA 2678 Subsequence(二分查找)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. LA 2678 – Subsequence

    看到限时3S,自己写了一个二重循环的,然后华丽的 TLE...T T 瞄了瞄书上,作者的思路果然是很好.膜拜中. 他只枚举了终点,然后用二分查找. 用到了lower_bound函数,这个lower_b ...

  3. LA 3029 Subsequence

    LA 3029 A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...

  4. [UVALive 2678] Subsequence

    图片加载可能有点慢,请跳过题面先看题解,谢谢 在切水题的道路上狂奔,一发不可收拾... 这道题好像不用写什么题解吧,吐个槽什么的算了 一眼题,大佬们都不屑于做,只有我这种弱菜才来写这种题目玩儿 记个前 ...

  5. 【UVALive】2678 Subsequence(尺取法)

    题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...

  6. SPOJ 274 Johnny and the Watermelon Plantation(TLE)

    O(n^3)的时间复杂度,改了半天交了二三十遍,TLE到死,实在没办法了…… 跪求指点!!! #include <cstdio> #include <cstdlib> #inc ...

  7. 【uva 1471】Defense Lines(算法效率--使用数据结构+部分枚举+类贪心)

    P.S.我完全一个字一个字敲出来的血泪史啊~~所以,没有附代码,也是可以理解的啦.OvO 题意:给一个长度为N(N≤200000)的序列,要删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增 ...

  8. HDU 1159:Common Subsequence(LCS模板)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

随机推荐

  1. ajax原理总结附简单实例及其优点

    在工作中用了Ajax N多次了,也看过一些相关方面的书籍,也算是认识了它,但是一直没有认真总结和整理过相关的东东,失败! 近有闲情,将之总结如下: [名称] Ajax是Asynchronous Jav ...

  2. win8系统输入法设置

    Windows 8系统自带微软拼音简捷输入法,无论是在Windows的开始屏幕新界面中还是Windows传统桌面里,按Shift键或者直接点击屏幕上的"中/英"标识即可切换中英文输 ...

  3. Delphi中有序型

    有序类型包括:.integer(整型).character(字符型).boolean(布尔型).enumerated(枚举型).subrange(子界型)有序类型定义了一组被排序的值.每个相异值都有唯 ...

  4. POJ 2226

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7557   Accepted: 2791 Desc ...

  5. 错误 1 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口

    http://www.cnblogs.com/waitingfor/archive/2011/12/19/2293469.html

  6. JSONObject 包的依赖

    commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json- ...

  7. -ffunction-sections -Wl,--gc-sections

    AVR/GCC设置不链接未调用的函数 http://blog.csdn.net/shevsten/article/details/7049688 在AVR Studio4/5的AVR/GCC默认设置下 ...

  8. 关于java中split的使用

    之前在http://shukuiyan.iteye.com/blog/507915文中已经叙述过这个问题,但是最近一次笔试中居然有碰到了这个知识点,而且还做错了,囧!学艺不精啊.题目大概是这样的: ) ...

  9. QT visual stuido 集成插件不能打开ui文件的解决方法(去掉xml的UTF8标记)

    QT visual stuido 集成插件不能打开ui文件的解决方法 visual studio里不能打开这个ui文件,出现warning等解决方法是:于是将<?xml version=&quo ...

  10. CSS3通配符

    在 CSS3 中,追加了三个属性选择器分别为: [att*=val] ----内容包含 [att^=val] ----开头匹配 [att$=val] ----结尾匹配 示例: <!DOCTYPE ...