Codeforces13E - Holes

本文详细解析了一种基于块划分的高效算法,用于解决弹球游戏中的路径追踪问题。通过将洞穴分为多个块,算法能在O(m√n)的时间复杂度下处理大量操作,包括洞穴力量值的修改及球的弹射路径预测。文章提供了完整的代码实现,并讨论了算法的优化策略。

Portal

Description

\(n(n\leq10^5)\)个洞排成一条直线,第\(i\)个洞有力量值\(a_i\),当一个球掉进洞\(i\)时就会被立刻弹到\(i+a_i\),直到超出\(n\)。进行\(m(m\leq10^5)\)次操作:

  • 修改第\(i\)个洞的力量值\(a_i\)
  • 在洞\(x\)上放一个球,问该球几次后被哪个洞弹飞出界。

Solution

\(n\)个洞分成大小为\(\sqrt n\)\(\sqrt n\)个块。
\(c[i]\)记录\(i\)要跳出所在的块需要多少次,\(nxt[i]\)记录跳出到哪个点。
修改时,从后到前重构该块内所有点的\(c[i]\)\(nxt[i]\),其他块不受影响。
查询时,由\(i\)跳到\(nxt[i]\)并累加\(c[i]\),在即将出界\((nxt[i]>n)\)前一步一步跳来得知是哪个洞将它弹出界的。

时间复杂度\(O(m\sqrt n)\)

Code

//Holes
#include <cstdio>
#include <cmath>
inline char gc()
{
    static char now[1<<16],*S,*T;
    if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
    return *S++;
}
inline int read()
{
    int x=0,f=1; char ch=gc();
    while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=gc();}
    while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int const N=1e5+10;
int n,m,n0;
int p[N],nxt[N],c[N];
void update(int t)
{
    int fr=t*n0,to=fr+n0-1; if(to>n) to=n;
    for(int i=to;i>=fr;i--)
        if(i+p[i]>to) nxt[i]=i+p[i],c[i]=1;
        else nxt[i]=nxt[i+p[i]],c[i]=1+c[i+p[i]];
}
int main()
{
    n=read(),m=read(); n0=sqrt(n);
    for(int i=1;i<=n;i++) p[i]=read(),nxt[i]=i,c[i]=0;
    for(int t=0;t<=n/n0;t++) update(t);
    for(int i=1;i<=m;i++)
    {
        int opt=read();
        if(opt==0)
        {
            int x=read(),y=read();
            p[x]=y; update(x/n0);
        }
        else
        {
            int res=0,pre;
            for(int x=read();x<=n;x=nxt[x]) pre=x,res+=c[x];
            while(pre+p[pre]<=n) pre+=p[pre];
            printf("%d %d\n",pre,res);
        }
    }
    return 0;
}

P.S.

双倍经验[Hnoi2010]Bounce 弹飞绵羊

转载于:https://www.cnblogs.com/VisJiao/p/8490839.html

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值