码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Codeforces Round #833 (Div. 2) C. Zero-Sum Prefixes


    Codeforces Round #833 (Div. 2) C. Zero-Sum Prefixes

    Let’s consider the prefix sum array s = [ a 1 , a 1 + a 2 , … , a 1 + a 2 + … + a n ] s=[a_1,a_1+a_2,\ldots,a_1+a_2+\ldots+a_n] s=[a1​,a1​+a2​,…,a1​+a2​+…+an​].

    For every index i i i such that a i = 0 a_i=0 ai​=0, if we change the value of a i a_i ai​ to x x x, then every element from the suffix [ s i , s i + 1 , … , s n ] [s_i,s_{i+1},\ldots,s_n] [si​,si+1​,…,sn​] will be increased by x x x. Therefore, if a i 1 = a i 2 = … = a i k = 0 a_{i_1}=a_{i_2}=\ldots=a_{i_k}=0 ai1​​=ai2​​=…=aik​​=0, we’ll partition array s s s into multiple subarrays:

    • [ s 1 , s 2 , … , s i 1 − 1 ] [s_1,s_2,\ldots,s_{i_1-1}] [s1​,s2​,…,si1​−1​];
    • [ s i 2 , s i 2 + 1 , … , s i 3 − 1 ] [s_{i_2},s_{i_2+1},\ldots,s_{i_3-1}] [si2​​,si2​+1​,…,si3​−1​];
      … \ldots …
    • [ s i k , s i k + 1 , … , s n ] [s_{i_k},s_{i_{k+1}},\ldots,s_n] [sik​​,sik+1​​,…,sn​];
      Since none of the elements from the first subarray can be changed, it will contribute with the number of occurences of 0 0 0 in [ s 1 , s 2 , … , s i 1 − 1 ] [s_1,s_2,\ldots,s_{i_1-1}] [s1​,s2​,…,si1​−1​] towards the final answer.

    For each of the other subarrays [ s l , s l + 1 , … , s r ] [s_l,s_{l+1},\ldots,s_r] [sl​,sl+1​,…,sr​], let x x x be the most frequent element in the subarray, appearing f r [ x ] fr[x] fr[x] times. Since a l = 0 a_l=0 al​=0, we can change the value of a l a_l al​ to − x -x −x. In this case, every x x x in this subarray will become equal to 0 0 0, and our current subarray will contribute with f r [ x ] fr[x] fr[x] towards the final answer.

    Time complexity per testcase: O ( N l o g N ) O(NlogN) O(NlogN)

    #include
    using namespace std;
    
    typedef long long LL;
    
    int main()
    {
        int T;cin>>T;
        while(T--)
        {
            int n;cin>>n;
    
            map<LL,LL> b;
            bool ok_zero=false;
            LL sum=0,mx=0,res=0;
    
            for(int i=0;i<n;i++)
            {
                int x;cin>>x;
    
                if(x==0)
                {
                    if(ok_zero) res+=mx;
                    else res+=b[0];
    
                    ok_zero=true;
                    mx=0;
                    b.clear();
                }
    
                sum+=x;
                b[sum]++;
                mx=max(mx,b[sum]);
            }
    
            if(ok_zero) res+=mx;
            else res+=b[0];
    
            cout<<res<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    微信公众号消息接入(普通消息+模板消息)
    量化交易之日内回转策略:如何利用MACD指标实现盈利?
    PSP - 蛋白质复合物结构预测 Template 的 Multichain Mask 2D (二维多链掩码)
    vue3-新特性
    面试经典 150 题 2 —(双指针)— 392. 判断子序列
    [附源码]Python计算机毕业设计Django蛋糕购物商城
    corosync+packmaker+drbd+nfs高可用存储
    日常Git使用自我记录
    牛客题目——滑动窗口的最大值、矩阵最长递增路径、顺时针旋转矩阵、接雨水问题
    快速上手JSON数据传递&异常处理
  • 原文地址:https://blog.csdn.net/qq_52792570/article/details/127829419
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号