zoukankan      html  css  js  c++  java
  • Balanced Playlist from Codeforces Global Round 5

    https://codeforces.com/contest/1237/problem/D

    i want to remind of the diversity of solutions to this facinating problem.

    description

    you have a playlist consists of (n) CDs. the playlist is automatic and cyclic, which means once you choose to start with (i)-th CD, whenever it is finished, CD number (i+1) (if (i<n)) or (1) (if (i=n)) will start playing automatically.

    each CD is assigned a coolness as you like. once you start music, at any moment, you will remember the maximum coolness (x) you have ever heard. you party will terminate the moment you are going to go through a CD with coolness less than (x/2).

    for every CD you play initially, find how many CDs you will listen before you turn off the music.

    breakthough

    update the second loop

    first we have to generate an idea of brute force in O((n^2)), then we can try optimization.

    when the answer of (i)-th CD is calculated, according to our brute Force we have found out the terminal and we rename it as (j). which leads to (ans=j-i). meanwhile, we denote (f[i]=j) as the terminal of (i)-th CD

    note that we have recalculate something in the second loop, which means that between (i) and (i+1) some iterations of (j) overlap. we need to clarify how it happens.

    denote (max) as the maximum coolness of CDs between (i) and (j), denote (maxPos) as where (max) locates. then it occurs to us that for any (k) from (i) to (maxPos), (f[k]=f[i]=j). that is the redundancy and an oppotunity for us to improve the following:

    • just skip this or directly jump from (i) to (max+1) in the first loop.

    what about (k) from (max+1) to (j)? since the limitation of (j) is removed the moment (max) is out, it is obvious that (f[k]>f[i]=j) . here is the other improvement:

    • let the pointer (j) in the second loop stays there instead of refreshing it to be (i+1)

    implement both and you will get this problem passed.

    to calculate the max element of a segment, use segment tree or RMQ, or
    data structure of treap like map or priority queue

  • 相关阅读:
    LSTM模型与前向反向传播算法
    循环神经网络(RNN)模型与前向反向传播算法
    卷积神经网络(CNN)反向传播算法
    卷积神经网络(CNN)前向传播算法
    卷积神经网络(CNN)模型结构
    深度神经网络(DNN)的正则化
    深度神经网络(DNN)损失函数和激活函数的选择
    ubuntu下如何编译openthread?
    linux下如何删除空行?
    vi下什么快捷键可以完成大小写转换?
  • 原文地址:https://www.cnblogs.com/reshuffle/p/12547385.html
Copyright © 2011-2022 走看看