题意:求最小的长度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. 重置CentOS6.5的登录口令

    早先在虚拟机Vmware里安装了一台CentOS6.5,现在想登录,发现无论怎么输入登录口令都不正确,以至于无法登录. 查阅网上资料,可用下面步骤里的方法重置登录口令,在此记录. 1.启动机器,出现下 ...

  2. Solidity 合约调用合约

    原文地址:https://medium.com/@k3no/making-a-birthday-contract-858fd3f63618 先将datetime合约部署:https://github. ...

  3. rest-framework之序列化组件

    一:django自带序列化组件 Django内置的serializers(把对象序列化成json字符串) from django.core import serializers def test(re ...

  4. ThinkPHP5权限控制

    我在用ThinkPHP5做开发的时候发现,它没有权限类,自己写太麻烦,于是就想到了把TP3里面的权限类拿来修改使用,结果这种方法是可行的,下面记录附上修改后的Auth.php权限类 <?php ...

  5. springcloud 实现微服务间调用

    package com.idoipo.ibt.config; import org.apache.http.HttpException; import org.apache.http.HttpRequ ...

  6. TSQL--删除正在运行的数据库

    ); SET @dbName='DB1_SNAP' BEGIN TRY --===================================== --查找当前数据库所有连接并删除 DECLARE ...

  7. SurfaceView和SurfaceHolder的基本用法

    仅做记录使用,新手也可以来看看,怎么得到一个surfaceholder. 1.在xml文件中增加一个surfaceView控件. <SurfaceView android:layout_widt ...

  8. Xamarin.Forms之UserDialogs 重制版本

    在 forms 里面,目前使用比较多的弹出组件是 Acr.UserDialogs ,但是这个组件有些小问题,比如 loading .hide 会同时把 toast 给一起关掉,android 下的 t ...

  9. Castle Windsor 注册组件

    1.逐个注册组件即对每个接口通过代码指定其实现类,代码: container.Register( Component.For<IMyService>() //接口 .Implemented ...

  10. pdo 预处理

      <?php /* * pdo 预处理sql */ $dsn = "mysql:dbname=0328;host=localhost"; $username = " ...