LA 2678 Subsequence
有一个正整数序列,求最短的子序列使得其和大于等于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的更多相关文章
- LA 2678 Subsequence(二分查找)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- LA 2678 – Subsequence
看到限时3S,自己写了一个二重循环的,然后华丽的 TLE...T T 瞄了瞄书上,作者的思路果然是很好.膜拜中. 他只枚举了终点,然后用二分查找. 用到了lower_bound函数,这个lower_b ...
- LA 3029 Subsequence
LA 3029 A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...
- [UVALive 2678] Subsequence
图片加载可能有点慢,请跳过题面先看题解,谢谢 在切水题的道路上狂奔,一发不可收拾... 这道题好像不用写什么题解吧,吐个槽什么的算了 一眼题,大佬们都不屑于做,只有我这种弱菜才来写这种题目玩儿 记个前 ...
- 【UVALive】2678 Subsequence(尺取法)
题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...
- SPOJ 274 Johnny and the Watermelon Plantation(TLE)
O(n^3)的时间复杂度,改了半天交了二三十遍,TLE到死,实在没办法了…… 跪求指点!!! #include <cstdio> #include <cstdlib> #inc ...
- 【uva 1471】Defense Lines(算法效率--使用数据结构+部分枚举+类贪心)
P.S.我完全一个字一个字敲出来的血泪史啊~~所以,没有附代码,也是可以理解的啦.OvO 题意:给一个长度为N(N≤200000)的序列,要删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增 ...
- HDU 1159:Common Subsequence(LCS模板)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
随机推荐
- hdu 2571 命运(递推,请小心)
题目 //不能广搜,会超内存//可以用dp思想模拟//map 后来保存的是 保存由前面推来的最大的幸运总值的点//下标从1开始,不然倍数会有问题 //AC 代码: AC代码 //不能广搜,会超内存 / ...
- poj 3684
Physics Experiment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 784 Accepted: 266 ...
- POJ 2101
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int ...
- Android——横屏和竖屏的切换,以及明文密码的显示
查看API文档: android.content.pm.ActivityInfo 在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏. ...
- 2014多校第七场1003 || HDU 4937 Lucky Number
题目链接 题意 : 给定一个十进制n,让你转化成某个进制的数,让这个数只包含3 4 5 6这些数字,这个进制就成为n的幸运数字,输出有多少幸运数字,例如19,5进制表示是34,所以5是19的一个幸运数 ...
- python unittest基本介绍
python内部自带了一个单元测试的模块,pyUnit也就是我们说的:unittest 1.介绍下unittest的基本使用方法: 1)import unittest 2)定义一个继承自unittes ...
- Template
创建win32应用程序空工程 //main.cpp//time: 01/08/2013 #include<d3d9.h>#include <d3dx9.h> #pragma c ...
- lintcode :同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- hdu 1515 Anagrams by Stack
题解: 第一:两个字符不相等(即栈顶字符与目标字符不相等):这种情况很容易处理,将匹配word的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...
- IOS底层数据结构--class
一.类的数据结构 Class(指针) typedef struct objc_class *Class; /* 这是由编译器为每个类产生的数据结构,这个结构定义了一个类.这个结构是通过编译器在执行时产 ...