博客
关于我
Codeforces Global Round 13 C. Pekora and Trampoline(思维/差分)
阅读量:327 次
发布时间:2019-03-04

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

要解决这个问题,我们需要找到将所有蹦床的弹力值减少到1所需的最少跳跃次数。以下是详细的解决步骤:

解题思路

  • 左到右遍历:我们从左到右逐个处理每个蹦床。左边的蹦床跳跃可以为右边的蹦床免费减少弹力值,这是减少跳跃次数的关键。
  • 计算跳跃次数:对于每个蹦床i,计算它本身需要跳跃的次数。如果s_i + i超过n,则只需将s_i减少到n - i。否则,需要考虑跳跃次数。
  • 差分数组维护:使用一个差分数组来维护每个跳跃对后续点的影响,避免直接模拟所有跳跃,提高效率。
  • 累加贡献:通过扫描差分数组,累加每个点的贡献,最终得到最少的跳跃次数。
  • 代码实现

    #include 
    using namespace std;typedef long long ll;#define ENDL "\n"const int maxn = 5e3 + 10;int a[maxn];ll cnt[maxn];int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) a[i] = 0; ll ans = 0; for (int i = 1; i <= n; i++) { cnt[i] += cnt[i - 1]; int res = 0; if (i + a[i] > n) { res += i + a[i] - n; a[i] = n - i; } res += a[i] - 1; if (cnt[i] > res) { cnt[i + 1] += cnt[i] - res; cnt[i + 2] -= cnt[i] - res; cnt[i + 2]++; cnt[i + a[i] + 1]--; } ans += max(0LL, res - cnt[i]); } cout << ans << ENDL; } return 0;}

    代码解释

  • 输入处理:读取输入数据,初始化变量。
  • 遍历每个蹦床:从左到右处理每个蹦床,维护一个差分数组cnt来记录每个点的贡献。
  • 计算跳跃次数:对于每个蹦床i,计算其需要跳跃的次数res,并更新差分数组。
  • 维护贡献:根据差分数组,更新后续点的贡献,避免重复计算。
  • 累加结果:计算每个点的贡献,累加到答案ans中。
  • 输出结果:输出最终的最少跳跃次数。
  • 这种方法通过差分数组高效地维护了每个跳跃对后续点的影响,确保了算法的高效性和正确性。

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

    你可能感兴趣的文章
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>