博客
关于我
bzoj4419: [Shoi2013]发微博 (三种做法)
阅读量:314 次
发布时间:2019-03-03

本文共 1648 字,大约阅读时间需要 5 分钟。

为了解决这个问题,我们需要计算每个用户在给定的微博操作记录后最终看到的消息数量。我们可以通过离线处理所有操作,记录每条操作对好友数量的影响,然后在处理发微博操作时,根据好友数量的变化来确定发送消息的用户。

方法思路

  • 离线处理操作:将所有操作记录下来,按时间逆序处理。
  • 记录好友数量变化:使用两个数组addremove分别记录每个加减操作对好友数量的影响。
  • 处理发微博操作:在逆序处理时,处理每条发微博操作,根据当前的好友数量,更新对应的用户收到的消息数量。
  • 具体步骤如下:

  • 初始化两个数组addremove,记录每个加减操作对好友数量的影响。
  • 逆序处理每条操作:
    • 对于加减操作,更新addremove数组。
    • 对于发微博操作,查询当前好友数量,更新消息数量。
  • 最终,消息数量数组即为每个用户收到的消息数量。
  • 解决代码

    def main():    import sys    input = sys.stdin.read().split()    ptr = 0    n = int(input[ptr])    ptr += 1    m = int(input[ptr])    ptr += 1    add = [0] * (n + 1)    remove = [0] * (n + 1)    operations = []    for _ in range(m):        op = input[ptr]        ptr += 1        x = int(input[ptr])        ptr += 1        y = int(input[ptr])        ptr += 1        operations.append((op, x, y))    # 初始化好友数量变化数组    current = [0] * (n + 1)    message = [0] * (n + 1)    # 逆序处理操作    for op in reversed(operations):        if op[0] == '!':            x = op[1]            # 发微博时,当前好友数量是处理完操作前的状态            cnt = current[x]            message[x] += cnt        elif op[0] == '+':            x = op[1]            y = op[2]            add[x] += 1            add[y] += 1            current[x] += 1            current[y] += 1        elif op[0] == '-':            x = op[1]            y = op[2]            remove[x] += 1            remove[y] += 1            current[x] -= 1            current[y] -= 1    # 输出结果    print(' '.join(map(str, message[1:])))if __name__ == "__main__":    main()

    代码解释

  • 读取输入:读取输入数据并解析操作记录。
  • 初始化数组addremove数组记录每个加减操作对好友数量的影响,message数组记录每个用户收到的消息数量。
  • 逆序处理操作:从最后一条操作开始逆序处理,处理发微博操作时,根据当前好友数量更新消息数量;处理加减操作时,记录对好友数量的影响。
  • 输出结果:打印每个用户收到的消息数量。
  • 通过这种方法,我们可以高效地处理大量操作,确保在合理的时间复杂度内解决问题。

    转载地址:http://ibcq.baihongyu.com/

    你可能感兴趣的文章
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>