UVALive - 2678 二分/尺取

题意:求最小的长度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;
} 

原文地址:https://www.cnblogs.com/caturra/p/8726785.html

时间: 2024-11-05 12:29:17

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

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 memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, i

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

题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an array B by a parameter K as following rules: Initially, the array B is empty. Consider each interval in array A. If the length of this interval is le

CodeForces - 237C Primes on Interval(二分+尺取)

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors. Consider positive integers a, a?+?1, ..., b (a?≤?b). You want to

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

http://acm.hdu.edu.cn/showproblem.php?pid=6119 [思路] 首先通过处理交叉的可以处理成不交叉的 然后二分查找答案 如何判断一个长度是否可行? 双指针O(n)扫一次就可以,要处理区间和问题,所以先求前缀和 时间复杂度O(nlogn) [AC] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #inclu

POJ3061 Subsequence 尺取or二分

Description 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 o

POJ-3061 Subsequence 二分或尺取

题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断是否可行,这样是O(nlgn)的   注意没有答案 ans输出0 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<cstring> 5 using namespace

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

<题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不错. 尺取: #include <bits/stdc++.h> using namespace std; int arr[int(1e5+5)]; int main(){ int T,n,k;scanf("%d",&T); while(T--){ scanf("

POJ 3061 Subsequence ( 二分 || 尺取法 )

题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用先构造前缀和的方式来进行接下来的操作 ( 任意子序列的和都能由某两个前缀和的差表示 ). 二分做法 ==> 我们枚举起点,对于每一个起点 St 二分查找看看 St 后面有没有前缀和是大于或者等于 [ St的前缀和 ] + S 的,如果有说明从当前起点开始有一个终点使得起终之和是大于或者等于 S 的,

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序列的长度是多少. 将相同颜色的下标存到对应颜色的容器中,比如ans[a[i]].push_back(i)就是将下标为i颜色为a[i]的数存到ans[a[i]]容器中. 对于每种颜色序列,尺取一下 在差距小于k的情况下取能取到的最大长度. 1 //#pragma comment(linker, "/S