题意:求最小的长度L满足该长度上的元素和大于等于S

最近dp做多了总有一种能用dp解决一切的错觉

二分长度解决

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e5+15;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
int n,s;
ll sum[maxn],a[maxn];
bool C(int len){
for(int i=1;i+len-1<=n;i++){
if(sum[i+len-1]-sum[i-1]>=s)return 1;
}
return 0;
}
int main(){
while(cin>>n>>s){
rep(i,1,n)scanf("%lld",&a[i]);
rep(i,1,n) sum[i]=sum[i-1]+a[i];
int l=1,r=n,ans;
while(l<r){
int mid=l+(r-l)/2;
if(C(mid))r=mid;
else l=mid+1;
}
println((C(l)?l:0));
}
return 0;
}

UVALive - 2678 二分/尺取的更多相关文章

  1. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  2. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  3. 【二分+尺取】HDU 6119 小小粉丝度度熊

    http://acm.hdu.edu.cn/showproblem.php?pid=6119 [思路] 首先通过处理交叉的可以处理成不交叉的 然后二分查找答案 如何判断一个长度是否可行? 双指针O(n ...

  4. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

  5. POJ3061 Subsequence 尺取or二分

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

  6. 1686 第K大区间(尺取+二分)

    1686 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. ...

  7. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)

    题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1;   这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...

  8. POJ-3061 Subsequence 二分或尺取

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

  9. HDU-4123-树形dp+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  2. mybatis spring 框架整合

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test user=LF password=LF <?xml versi ...

  3. dynamic和匿名类和var的混合使用 没提示照样点

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  4. 使用rpmbuild打包时不对文件进行strip操作

    使用rpmbuild打包时不对文件进行strip操作 摘自: https://www.ichenfu.com/2017/11/20/rpmbuild-not-strip/ By Chen Fu 发表于 ...

  5. web端测试点汇总

    前言 前面一篇文章讲解了app测试一些功能点.那么相应的也梳理一下web测试相关的功能的测试点吧,此篇文章只是给你们一个思路,如果要涉及web端每个测试点,基本不可能实现的,所以只是提供一个设计的思路 ...

  6. madvise、fadvise、posix_madvise和posix_fadvise函数的使用

    系统调用madvise的作用:建议内核如何使用指定段的内存.函数原型如下: #include <sys/mman.h> int madvise(void *addr, size_t len ...

  7. Bitmap压缩到指定尺寸大小,获取圆角、圆形图片

    /** * 使用Matrix将Bitmap压缩到指定大小 * @param bitmap * @param w * @param h * @return */ public static Bitmap ...

  8. 使用Filter对POST和GET方式的请求参数的进行统一解码

    一.过滤器 二.自定义request类

  9. 平台播放声音(ext.js)

    首先把需要的两个js文件放在public/core路径下 (文件已经上传到博客了) 音频文件放在文件一级目录 代码:JxCustom.loadAudio("wav/NG.wav") ...

  10. angular 中间人模式

    import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app ...