• 题意:有一组数,问子数组和最大不超过\(t\)的最多元素个数.

  • 题解:用数组模拟队列,不断的往里面放,队列中的元素之和大于\(t\),就不断地从队头弹出直到满足条件,维护一个最大值即可.

  • 代码:

    int n,t;
    int a[N];
    int q[N];
    int hh,tt=-1; int main() {
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    n=read(),t=read();
    for(int i=1;i<=n;++i) a[i]=read();
    int ans=0;
    int sum=0; for(int i=1;i<=n;++i){
    sum+=a[i];
    q[++tt]=a[i];
    if(sum<=t) ans=max(ans,tt-hh+1);
    while(sum>t && hh<=tt){
    sum-=q[hh++];
    }
    } printf("%d\n",ans); return 0;
    }

Codeforces Round #171 (Div. 2) B. Books (模拟队列)的更多相关文章

  1. Codeforces Round #515 (Div. 3) C. Books Queries (模拟)

    题意:有一个一维的书架,\(L\)表示在最左端放一本书,\(R\)表示在最右端放一本书,\(?\)表示从左数或从右数,最少数多少次才能得到要找的书. 题解:我们开一个稍微大一点的数组,从它的中间开始模 ...

  2. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  3. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  4. Codeforces Round #543 (Div. 2) D 双指针 + 模拟

    https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...

  5. Codeforces Round #398 (Div. 2) A. Snacktower 模拟

    A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...

  6. CodeForces Round #515 Div.3 C. Books Queries

    http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...

  7. Codeforces Round #171 (Div. 2)

    A. Point on Spiral 由于坐标\(.|x|.|y| \le 100\),所以可直接bfs计算. 若数据较大,需要找规律. B. Books 维护窗口\([l,r]\),使\(\sum_ ...

  8. Codeforces Round #237 (Div. 2) B题模拟题

    链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...

  9. Codeforces Round #371 (Div. 2) C 大模拟

    http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...

随机推荐

  1. HAProxy + keepalived 高可用集群代理

    HAProxy + keepalived # 1 安装keepalived: yum install keepalived -y # 2 修改KEEPalived配置文件: vim /etc/keep ...

  2. Hbase snapshot数据迁移

    # 在源集群中创建快照(linux shell) hbase snapshot -t <table_name> -n <snapshot_name> 或(hbase shell ...

  3. pidof

    pidof 服务名称,就可以查看到服务占用的进程号

  4. 5.1中repair table

    mysql> repair table xs;+---------+--------+----------+----------+| Table | Op | Msg_type | Msg_te ...

  5. 【Git】2、Linux快速安装Git环境 & oh-my-zsh

    Linux快速安装Git环境 文章目录 Linux快速安装Git环境 1.Linux安装Git 2.安装zsh 3.安装oh-my-zsh 3.1.安装oh-my-zsh 3.2. 测试验证 4.小结 ...

  6. kubernets之存活探针

    一   存活探针存在的意义 1.1  kubernet通过存活探针(liveness probe)检查容器是否还在运行,可以为pod中的每个容器单独指定存活探针,如果探针执行失败,kubernets会 ...

  7. 数据分析 Pandas 简介和它的的数据结构

    本文主要讲Pandas 的Series和DataFrame 的相关属性和操作 1.Series的相关属性和操作# --Series是一种类似于一维数组的对象,只能存放一维数组!由以下两部分组成:# v ...

  8. IDEA安装codota插件和使用,开发人员的知心伙伴

    打开IDEA 点击左上角的File之后,如下图 成功后如图所示

  9. libuv工作队列

    目录 1.说明 2.API 2.1.uv_queue_work 2.2.uv_cancel 3.代码示例 1.说明 libuv 提供了一个线程池,可用于运行用户代码,libuv 中的工作队列中的任务会 ...

  10. git的使用学习笔记---分支删除

    一.使用场景: 1.修改bug,原来分支不管用 2,分支太多不易管理 二.方法 git branch -d branch1 无法删除:原因在与该分支为目前工作的分支,所以要切换分支 git check ...