【二分答案nlogn/标解O(n)】【UVA1121】Subsequence
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S <
100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.
Input
Many test cases will be given. For each test case the program has to read the numbers N and S, separated by an interval, from the first line.
The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.
Output
For each the case the program has to print the result on separate line of the output file. If there isn't such a subsequence, print 0 on a line by itself.
Sample Input
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3
有n个正整数组成一个序列。给定整数S,求长度最短的连续序列,使它们的和大于或等于S。
【输入格式】
输入包含多组数据。每组数据的第一行为整数n和S(10<n≤100 000,S<109);第二行为n个正整数,均不超过10 000。输入结束标志为文件结束符(EOF)。
【输出格式】
对于每组数据,输出满足条件的最短序列的长度。如果不存在,输出0。
二分答案(即序列长度)+前缀和可以轻松搞定
复杂度为O(n*logn)
(这个方法可以解决数可能为负数的数据,所以也是一种值得记忆的方法)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
int n,S;
int A[100010];
int ans;
int OK(int m)
{
if(S==0) return 1;
int temp=0;
for(int i=1;i<=n-(m-1);i++)
{
temp=A[i+(m-1)]-A[i-1];
if(S<=temp) return 1;
}
return 0;
}
void solve()
{
ans=0;
int l=1,r=n;
int m=(l+r)/2;
while(l<r)
{
if(OK(m)) r=m;
else l=m+1;
m=(l+r)/2;
}
ans=m;
}
void input()
{
for(int i=1;i<=n;i++)
{
scanf("%d",&A[i]);
A[i]=A[i-1]+A[i];
}
}
int main()
{
while(scanf("%d%d",&n,&S)!=EOF)
{
input();
solve();
if(ans!=n)
printf("%d\n",ans);
else if(A[n]<S)
{
printf("0\n");
}
else printf("%d\n",n);
}
return 0;
}
【二分答案nlogn/标解O(n)】【UVA1121】Subsequence的更多相关文章
- 3月28日考试 题解(二分答案+树形DP+数学(高精))
前言:考试挂了很多分,难受…… --------------------- T1:防御 题意简述:给一条长度为$n$的序列,第$i$个数的值为$a[i]$.现让你将序列分成$m$段,且让和最小的一段尽 ...
- [USACO]地震 (二分答案+最优比率生成树详解)
题面:[USACO 2001 OPEN]地震 题目描述: 一场地震把约翰家的牧场摧毁了, 坚强的约翰决心重建家园. 约翰已经重建了N个牧场,现在他希望能修建一些道路把它们连接起来.研究地形之后,约翰发 ...
- P3105 [USACO14OPEN]公平的摄影(正解是乱搞,我却二分了)(+二分答案总结)
照例化简题意: 给定一个01区间,可以把0改成1,问其中最长的01数量相等的区间长度. 额很容易想到前缀和,把w弄成1,h弄成-1,然后求前缀和,然后乱搞就行了. 但是一直不太会乱搞的我却直接想到了二 ...
- [NOIP2011] 聪明的质检员(二分答案)
题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...
- Codeforces Round #377 (Div. 2) D. Exams(二分答案)
D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...
- BZOJ 2525 Poi2011 Dynamite 二分答案+树形贪心
题目大意:给定一棵树,有一些点是关键点,要求选择不超过mm个点.使得全部关键点到近期的选择的点距离最大值最小 二分答案,问题转化为: 给定一棵树,有一些点是关键点,要求选择最少的点使得每一个关键点到选 ...
- 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑
Assemble Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3171 Accepted: 1013 Descript ...
- Codeforces 700A As Fast As Possible(二分答案)
[题目链接] http://codeforces.com/problemset/problem/700/A [题目大意] 有一辆限载k人速度为v2的车,n个步行速度均为v1的人要通过一段长度为l的距离 ...
- [BZOJ 2500]幸福的道路 树形dp+单调队列+二分答案
考试的时候打了个树链剖分,而且还审错题了,以为是每天找所有点的最长路,原来是每天起点的树上最长路径再搞事情.. 先用dfs处理出来每个节点以他为根的子树的最长链和次长链.(后面会用到) 然后用类似dp ...
随机推荐
- Impala与Hive的比較
1. Impala架构 Impala是Cloudera在受到Google的Dremel启示下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批 ...
- oracle开启/关闭归档模式
1.改变非归档模式到归档模式: 1)SQL> conn / as sysdba (以DBA身份连接数据库) 2)SQL> shutdown immediate;(立即关闭数据库) 3)SQ ...
- class创建单击事件
$(function () { $(".search-button").click(function () { $(" ...
- for()循环
今天发现自己一直以来都搞错了for()循环的执行顺序.这么简单的问题一直都错了,我也是醉了. ;i>&&a[i]>a[i-];--i) { } //即 for(init_s ...
- Gengxin讲STL系列——Queue和Stack
第三篇. 感觉队列和栈是必须的……所以决定加上这两个…… 我发现我已经买域名买上隐了……今天又买了个.top……真是智障…… Queue(队列FIFO)和Statk(栈LIFO). 那么为什么要这两个 ...
- [C++程序设计]多维数组元素的地址
设有一个二维数组a,它有3行4列.它的定义为int a[3][4]={{1,3,5,7},{9,11,13,15},{17,18,21,23}};a是一个数组名.a数组包含3行,即3个元 素:a[0] ...
- PHP 获取客户端IP
function get_ip() { static $realIP; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_ ...
- PAT 大数运算
PAT中关于大数的有B1017,A1023,A1024 (A-Advance,B-Basic) B1017 1017. A除以B (20) 本题要求计算A/B,其中A是不超过1000位的正整数,B是1 ...
- linux curses函数库
fedora20,安装yum install ncurses-devel 编译时:-lncurses 头文件:#include<curses.h> 参考:man ncurses \linu ...
- Cows(poj 2481 树状数组)
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15301 Accepted: 5095 Description ...