ACM训练赛第19场补题

这篇博客介绍了如何解决ACM训练赛中的一个问题——计算Blake在每个连续级别区间中达到或超过目标平均分的次数。通过使用树状数组(权值树状数组)的数据结构,博主提供了算法实现来高效地找出所有符合条件的区间,并给出了样例输入和输出。算法主要涉及数组处理、前缀和计算和区间平均值的计算,对于理解和应用动态数据结构有一定的参考价值。

ACM训练赛第19场补题

问题 A: At Least Average

时间限制: 1 Sec 内存限制: 128 MB
题目描述
Blake plays a video game that has n levels. On each level, she can earn in between 0 and 10 points, inclusive. The number of points she gets on a level must be an integer. She has set up a goal for herself to equal or beat a particular average. In order to make her performance seem impressive, she’s decided that she wants to count the number of intervals (sets of consecutive levels) where she’s equaled or beaten her target average.

We define an interval [i, j], with 1 ≤ i ≤ j ≤ n, to be each level starting at level i and ending at level j, including level j. For example, if Blake played 5 levels with her scores being 5, 1, 2, 4 and 6, and Blake’s target average was 3, then here are the following intervals where she equaled or beat her target average:

[1, 1] with average 5
[1, 2] with average 3
[1, 4] with average 3
[1, 5] with average 3.6
[2, 5] with average 3.25
[3, 4] with average 3
[3, 5] with average 4
[4, 4] with average 4
[4, 5] with average 5
[5, 5] with average 6

Blake can make the impressive statement that on 10 intervals her average score was 3 or more.

The Problem
Given the number of levels Blake has played in her video game, her scores on each level, and the average value she’d like to equal or beat, determine the number of intervals that she equaled or beat the given average.
输入
The first line of input will consist of a single positive integer, v (v ≤ 10), representing the number of input cases to process. Input for each case follows, one case taking two lines. The first line of input for each case contains two positive integers, n (n ≤ 100000), and a (a ≤ 10), separated by spaces, representing the number of levels of the video game and the average Blake wants to obtain, respectively. The following line contains n space separated integers, each in between 0 and 10 inclusive, representing Blake’s score on each of the n levels, in order.
输出
For each input case, output a single integer on a line by itself representing the number of intervals where Blake obtained her desired average or higher.
样例输入 Copy
2
5 3
5 1 2 4 6
9 6
10 1 10 1 10 1 10 1 10
样例输出 Copy
10
15
思路
求某个数组里有多少个区间的区间平均值大于等于某个固定的数。
权值树状数组。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int MAXN = 2000000 + 10;
const int base = 1000000;
int n;
int a[MAXN];
long long sum[MAXN];
int c[MAXN];
int lowbit(int x) {
    return x & (-x);
}
 
int query(int x) {
    int ret = 0;
    while (x > 0) {
        ret += c[x];
        x -= lowbit(x);
    }
    return ret;
}
 
void update(int i, int k) {
    while (i <= 2000008) {
        c[i] += k;
        i += lowbit(i);
    }
}
 
int main() {
    int T;
    scanf("%d", &T);
    while (T --) {
        memset(c,0,sizeof(c));
        memset(sum,0,sizeof(sum));
        scanf("%d", &n);
        int avg;
        scanf("%d", &avg);
        for (int i = 1; i <= n; i ++) {
            scanf("%d", &a[i]);
            a[i] = a[i] - avg ; // 将所有数减去平均值,看有多少数大于等于零 
        }
        for (int i = 1; i <= n; i ++) {
            sum[i] = sum[i - 1] + a[i]; // 求前缀和 
        }
        for(int i = 1; i <= n; i ++) {
			sum[i] += base; // 防止负数,加上某个数变成正数 
        }
        update(base, 1);
        long long cnt = 0;
        for (int i = 1; i <= n; i ++) {
            cnt += query(sum[i]); // 小于等于当前数的数量个数 
    //      cout << sum[i] << " " << query(sum[i]) << endl;
            update(sum[i] , 1); // 使树状数组sum[i]下标位置加1 
        }
        cout << cnt << endl;
    }
    return 0;
}
/**************************************************************
    Problem: 19121
    User: 2021Team066
    Language: C++
    Result: 正确
    Time:276 ms
    Memory:64524 kb
****************************************************************/
已经博主授权,源码转载自 https://pan.quark.cn/s/053f1da40351 在计算机科学领域,MIPS(Microprocessor without Interlocked Pipeline Stages)被视作一种精简指令集计算机(RISC)的架构,其应用广泛存在于教学实践和嵌入式系统设计中。 本篇内容将深入阐释MIPS汇编语言中涉及数组处理的核心概念与实用操作技巧。 数组作为一种常见的数据结构,在编程中能够以有序化的形式储存及访问具有相同类型的数据元素集合。 在MIPS汇编语言环境下,数组通常借助内存地址与索引进行操作。 以下列举了运用MIPS汇编处理数组的关键要素:1. **数据存储**: - MIPS汇编架构采用32位地址系统,从而能够访问高达4GB的内存容量。 - 数组元素一般以连续方式存放在内存之中,且每个元素占据固定大小的字节空间。 例如,针对32位的整型数组,其每个元素将占用4字节的存储空间。 - 数组首元素的地址被称为基地址,而数组任一元素的地址可通过基地址加上元素索引乘以元素尺寸的方式计算得出。 2. **寄存器运用**: - MIPS汇编系统配备了32个通用寄存器,包括$zero, $t0, $s0等。 其中,$zero寄存器通常用于表示恒定的零值,$t0-$t9寄存器用于暂存临时数据,而$s0-$s7寄存器则用于保存子程序的静态变量或参数。 - 在数组处理过程中,基地址常被保存在$s0或$s1寄存器内,索引则存储在$t0或$t1寄存器中,运算结果通常保存在$v0或$v1寄存器。 3. **数组操作指令**: - **Load/Store指令**:这些指令用于在内存与寄存器之间进行数据传输,例如`lw`指令用于加载32位数据至寄存器,`sw`指令...
根据原作 https://pan.quark.cn/s/cb681ec34bd2 的源码改编 基于Python编程语言完成的飞机大战项目,作为一项期末学习任务,主要呈现了游戏开发的基本概念和技术方法。 该项目整体构成约500行代码,涵盖了游戏的核心运作机制、图形用户界面以及用户互动等关键构成部分。 该项目配套提供了完整的源代码文件、相关技术文档、项目介绍演示文稿以及运行效果展示视频,为学习者构建了一个实用的参考范例,有助于加深对Python在游戏开发领域实际应用的认识。 我们进一步研究Python编程技术在游戏开发中的具体运用。 Python作为一门高级编程语言,因其语法结构清晰易懂和拥有丰富的库函数支持,在开发者群体中获得了广泛的认可和使用。 在游戏开发过程中,Python经常与Pygame库协同工作,Pygame是Python语言下的一款开源工具包,它提供了构建2D游戏所需的基础功能模块,包括窗口系统管理、事件响应机制、图形渲染处理、音频播放控制等。 在"飞机大战"这一具体游戏实例中,开发者可能运用了以下核心知识点:1. **Pygame基础操作**:掌握如何初始化Pygame环境,设定窗口显示尺寸,加载图像和音频资源,以及如何启动和结束游戏的主循环流程。 2. **面向对象编程**:游戏中的飞机、子弹、敌人等游戏元素通常通过类的设计来实现,利用实例化机制来生成具体的游戏对象。 每个类都定义了自身的属性(例如位置坐标、移动速度、生命值状态)和方法(比如移动行为、碰撞响应、状态更新)。 3. **事件响应机制**:Pygame能够捕获键盘输入和鼠标操作事件,使得玩家可以通过按键指令来控制飞机的移动和射击行为。 游戏会根据这些事件的发生来实时更新游戏景状态。 4. **图形显示与刷新**:...
【顶级SCI复现】高比例可再生能源并网如何平衡灵活性与储能成本?虚拟电厂多时间尺度调度及衰减建模(Matlab代码实现)内容概要:本文围绕高比例可再生能源并网背景下虚拟电厂的多时间尺度调度与储能成本优化问题展开研究,重点探讨如何在保证系统灵活性的同时降低储能配置与运行成本。通过构建多时间尺度(如日前、日内、实时)协调调度模型,并引入储能设备衰减建模,提升调度精度与经济性。研究结合Matlab代码实现,复现顶级SCI论文中的优化算法与建模方法,涵盖鲁棒优化、分布鲁棒、模型预测控制(MPC)等先进手段,兼顾风光出力不确定性与需求响应因素,实现虚拟电厂内部多能源协同优化。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源、智能电网、能源互联网领域的工程技术人员。; 使用景及目标:① 掌握虚拟电厂多时间尺度调度的核心建模思路与实现方法;② 学习如何将储能寿命衰减纳入优化模型以提升经济性;③ 复现高水平SCI论文中的优化算法与仿真流程,服务于科研论文写作与项目开发。; 阅读建议:建议结合文中提供的Matlab代码逐模块分析,重点关注目标函数设计、约束条件构建及求解器调用过程,配合实际案例数据进行调试与验证,深入理解优化模型与物理系统的映射关系。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值