博客
关于我
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/

    你可能感兴趣的文章
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>