Find first subarray sum to zero

本文介绍了一种算法,用于在包含正负数的数组中找到首个和为零的连续子数组。通过使用哈希表跟踪前缀和,算法在O(n)时间内解决了这一问题。

given an array with positive and negative numbers find the first continuous subarray that sums to 0


bool FindFirstPair(int& nStart, int& nEnd, int a[], int n)
{
        nStart = nEnd = n;
        int nCount = 0;
        hash_map<int, int> hash;
        bool bSet = false;
        for (int i = 0; i < n; i++)
        {
                nCount += a[i];
                if (0 == nCount)
                {
                        nStart = 0;
                        nEnd = i;
                        return true;
                }

                if (hash.find(nCount) == hash.end())
                        hash[nCount] = i;
                else
                {
                        int nIndex = hash[nCount];
                        if (nIndex+1 < nStart || (nIndex+1 == nStart && i - nIndex - 1 < nEnd - nStart))
                        {
                                nStart = nIndex+1;
                                nEnd = i;
                                bSet = true;
                        }
                }
        }

        return bSet;
}

有思路并不意味着做对,面试官看重 1,思路是否灵活;2,思维是否严谨


B. Serval and Final MEX time limit per test1 second memory limit per test256 megabytes You are given an array a consisting of n≥4 non-negative integers. You need to perform the following operation on a until its length becomes 1 : Select two indices l and r (1≤l<r≤|a| ), and replace the subarray [al,al+1,…,ar] with a single integer mex([al,al+1,…,ar]) , where mex(b) denotes the minimum excluded (MEX)∗ of the integers in b . In other words, let x=mex([al,al+1,…,ar]) , the array a will become [a1,a2,…,al−1,x,ar+1,ar+2,…,a|a|] . Note that the length of a decreases by (r−l) after this operation. Serval wants the final element in a to be 0 . Help him! More formally, you have to find a sequence of operations, such that after performing these operations in order, the length of a becomes 1 , and the final element in a is 0 . It can be shown that at least one valid operation sequence exists under the constraints of the problem, and the length of any valid operation sequence does not exceed n . Note that you do not need to minimize the number of operations. ∗ The minimum excluded (MEX) of a collection of integers b1,b2,…,bk is defined as the smallest non-negative integer x which does not occur in the collection b . Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows. The first line of each test case contains a single integer n (4≤n≤5000 ) — the length of the array a . The second line contains n integers a1,a2,…,an (0≤ai≤n ) — the elements of the array a . It is guaranteed that the sum of n over all test cases does not exceed 5000 . Output For each test case, output a single integer k (0≤k≤n ) in the first line of output — the length of the operation sequence. Then, output k lines, the i -th line containing two integers li and ri (1≤li<ri≤|a| ) — the two indices you choose in the i -th operation, where |a| denotes the length of the array be
03-23
### Problem Statement You are given a string $S$ of length $N$ consisting of `0` and `1`. You can perform the following operation any number of times (possibly zero): - Choose an integer $i$ satisfying $1 \leq i \leq N$, and change $S_i$ from `0` to `1`, or from `1` to `0`. Your goal is to make the `1`s in $S$ form **at most one** interval. Find the minimum number of operations required to achieve the goal. More precisely, the goal is to make $S$ such that there exists a pair of integers $(l,r)$ satisfying both of the following conditions. Find the minimum number of operations required to achieve the goal. - $1 \leq l \leq r \leq N+1$. - $S_i=$ `1` and $l \leq i < r$ are equivalent for each integer $i$ satisfying $1 \leq i \leq N$. It can be proved that the goal can always be achieved with a finite number of operations. $T$ test cases are given, so solve each. ### Constraints - $1 \leq T \leq 20000$ - $1 \leq N \leq 2 \times 10^5$ - $S$ is a string of length $N$ consisting of `0` and `1`. - For each input file, the sum of $N$ over all test cases is at most $2 \times 10^5$. - $T,N$ are integers. ### Input The input is given from Standard Input in the following format: ``` $T$ $\textrm{case}_1$ $\textrm{case}_2$ $\vdots$ $\textrm{case}_T$ ``` $\textrm{case}_i$ represents the $i$\-th test case and is given in the following format: ``` $N$ $S$ ``` ### Output Output $T$ lines. The $i$\-th line $(1 \leq i \leq T)$ should contain the answer for the $i$\-th test case ### Sample Input 3 5 10011 10 1111111111 7 0000000 ### Sample Output 1 0 0 In the first test case, if we perform the operation to change $S_1$ to `0`, the `1`s will form one interval. Also, the initial $S$ does not satisfy the condition. Therefore, the answer is $1$. In the second test case, there are no `0`s in $S$, so no operations need to be performed. Therefore, the answer is $0$. In the third test case, there are no `1`s in $S$, so no operations need to be performed. Therefore, the answer is $0$. ### Sample Input 5 2 01 10 1000010011 12 111100010011 3 111 8 00010101 ### Sample Output 0 2 3 0 2 写出这道题目的c++代码,并保证通过所有样例,运行时间再2s内,运行内存在256mb内
06-01
跟网型逆变器小干扰稳定性分析与控制策略优化研究(Simulink仿真实现)内容概要:本文围绕跟网型逆变器的小干扰稳定性展开分析,重点研究其在电力系统中的动态响应特性及控制策略优化问题。通过构建基于Simulink的仿真模型,对逆变器在不同工况下的小信号稳定性进行建模与分析,识别系统可能存在的振荡风险,并提出相应的控制优化方法以提升系统稳定性和动态性能。研究内容涵盖数学建模、稳定性判据分析、控制器设计与参数优化,并结合仿真验证所提策略的有效性,为新能源并网系统的稳定运行提供理论支持和技术参考。; 适合人群:具备电力电子、自动控制或电力系统相关背景,熟悉Matlab/Simulink仿真工具,从事新能源并网、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 分析跟网型逆变器在弱电网条件下的小干扰稳定性问题;② 设计并优化逆变器外环与内环控制器以提升系统阻尼特性;③ 利用Simulink搭建仿真模型验证理论分析与控制策略的有效性;④ 支持科研论文撰写、课题研究或工程项目中的稳定性评估与改进。; 阅读建议:建议读者结合文中提供的Simulink仿真模型,深入理解状态空间建模、特征值分析及控制器设计过程,重点关注控制参数变化对系统极点分布的影响,并通过动手仿真加深对小干扰稳定性机理的认识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值