11536 - Smallest Sub-Array

本文介绍了一种用于寻找包含指定元素集合最短连续子序列的高效算法。该算法通过一次遍历序列,并利用队列记录关键元素位置,实现O(n)的时间复杂度。

题目大意:

给一个序列

X1 = 1

X2 = 2

X3 = 3

Xi = (Xi-1 + Xi-2 + Xi-3) % M + 1         for i = 4 to N


求一段最短的连续子序列,使得这个子序列包含正整数【1,k】


思路:

扫描一遍即可,用一个队列记录下【1,k】区间内的数的位置,再用一个变量count维护【1,k】内不重复数的个数。当count等于k时说明当前序列已经满足了要求,而队列头的数的位置就是起始位置。

算法复杂度O(n)


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
int n,m,k;
int arr[MAXN];
int cnt[MAXN];
const int INF = 0x7f7f7f7f;
void init()
{
    arr[1]=1;
    arr[2]=2;
    arr[3]=3;
    for(int i=4;i<=n;i++)
        arr[i]=(arr[i-1]+arr[i-2]+arr[i-3])%m+1;
    memset(cnt,0,sizeof(cnt));
}
int main()
{
    int nCase, cas=1;
    scanf("%d",&nCase);
    while(nCase--){
        scanf("%d%d%d",&n,&m,&k);
        init();
        int minx=INF;
        int count=0;
        queue<int> Q;

        for(int i=1;i<=n;i++){
            if(arr[i]>=1 &&arr[i]<=k){
                Q.push(i);
                if(cnt[arr[i]]++ == 0) ++count;
            }
            while(count==k){
                int tmp = i-Q.front()+1;
                minx = min(tmp,minx);
                int val = arr[Q.front()];
                if(--cnt[val]==0){
                    --count;
                }
                Q.pop();
            }
        }
        printf("Case %d: ", cas++);
        if(minx == INF) puts("sequence nai");
        else printf("%d\n", minx);
    }
    return 0;
}


根据原作 https://pan.quark.cn/s/0ed355622f0f 的源码改编 野火IM解决方案 野火IM是专业级即时通讯和实时音视频整体解决方案,由北京野火无限网络科技有限公司维护和支持。 主要特性有:私有部署安全可靠,性能强大,功能齐全,全平台支持,开源率高,部署运维简单,二次开发友好,方便与第三方系统对接或者嵌入现有系统中。 详细情况请参考在线文档。 主要包括一下项目: 野火IM Vue Electron Demo,演示如何将野火IM的能力集成到Vue Electron项目。 前置说明 本项目所使用的是需要付费的,价格请参考费用详情 支持试用,具体请看试用说明 本项目默认只能连接到官方服务,购买或申请试用之后,替换,即可连到自行部署的服务 分支说明 :基于开发,是未来的开发重心 :基于开发,进入维护模式,不再开发新功能,鉴于已经终止支持且不再维护,建议客户升级到版本 环境依赖 mac系统 最新版本的Xcode nodejs v18.19.0 npm v10.2.3 python 2.7.x git npm install -g node-gyp@8.3.0 windows系统 nodejs v18.19.0 python 2.7.x git npm 6.14.15 npm install --global --vs2019 --production windows-build-tools 本步安装windows开发环境的安装内容较多,如果网络情况不好可能需要等较长时间,选择早上网络较好时安装是个好的选择 或参考手动安装 windows-build-tools进行安装 npm install -g node-gyp@8.3.0 linux系统 nodej...
function peaks = houghpeaks(varargin) %HOUGHPEAKS Identify peaks in the Hough transform. % PEAKS = HOUGHPEAKS(H,NUMPEAKS) locates peaks in the Hough % transform matrix, H, generated by the HOUGH function. NUMPEAKS % specifies the maximum number of peaks to identify. PEAKS is % a Q-by-2 matrix, where Q can range from 0 to NUMPEAKS. Q holds % the row and column coordinates of the peaks. If NUMPEAKS is % omitted, it defaults to 1. % % PEAKS = HOUGHPEAKS(...,PARAM1,VAL1,PARAM2,VAL2) sets various % parameters. Parameter names can be abbreviated, and case % does not matter. Each string parameter is followed by a value % as indicated below: % % 'Threshold' Nonnegative scalar. % Values of H below 'Threshold' will not be considered % to be peaks. Threshold can vary from 0 to Inf. % % Default: 0.5*max(H(:)) % % 'NHoodSize' Two-element vector of positive odd integers: [M N]. % 'NHoodSize' specifies the size of the suppression % neighborhood. This is the neighborhood around each % peak that is set to zero after the peak is identified. % % Default: smallest odd values greater than or equal to % size(H)/50. % % Class Support % ------------- % H is the output of the HOUGH function. NUMPEAKS is a positive % integer scalar. % % Example % ------- % Locate and display two peaks in the Hough transform of the % rotated circuit.tif image. % % I = imread('circuit.tif'); % BW = edge(imrotate(I,50,'crop'),'canny'); % [H,T,R] = hough(BW); % P = houghpeaks(H,2); % imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); % xlabel('\theta'), ylabel('\rho'); % axis on, axis normal, hold on; % plot(T(P(:,2)),R(P(:,1)),'s','color','white'); % % See also HOUGH and HOUGHLINES. % Copyright 1993-2003 The MathWorks, Inc. % $Revision: 1.1.8.2 $ $Date: 2004/10/20 17:54:16 $ % References: % Rafael C. Gonzalez, Richard E. Woods, Steven L. Eddins, "Digital % Image Processing Using MATLAB", Prentice Hall, 2004 [h, numpeaks, threshold, nhood] = parseInputs(varargin{:}); % initialize the loop variables done = false; hnew = h; nhood_center = (nhood-1)/2; peaks = []; while ~done [dummy max_idx] = max(hnew(:)); %#ok [p, q] = ind2sub(size(hnew), max_idx); p = p(1); q = q(1); if hnew(p, q) >= threshold peaks = [peaks; [p q]]; % add the peak to the list % Suppress this maximum and its close neighbors. p1 = p - nhood_center(1); p2 = p + nhood_center(1); q1 = q - nhood_center(2); q2 = q + nhood_center(2); % Throw away neighbor coordinates that are out of bounds in % the rho direction. [qq, pp] = meshgrid(q1:q2, max(p1,1):min(p2,size(h,1))); pp = pp(:); qq = qq(:); % For coordinates that are out of bounds in the theta % direction, we want to consider that H is antisymmetric % along the rho axis for theta = +/- 90 degrees. theta_too_low = find(qq < 1); qq(theta_too_low) = size(h, 2) + qq(theta_too_low); pp(theta_too_low) = size(h, 1) - pp(theta_too_low) + 1; theta_too_high = find(qq > size(h, 2)); qq(theta_too_high) = qq(theta_too_high) - size(h, 2); pp(theta_too_high) = size(h, 1) - pp(theta_too_high) + 1; % Convert to linear indices to zero out all the values. hnew(sub2ind(size(hnew), pp, qq)) = 0; done = size(peaks,1) == numpeaks; else done = true; end end %----------------------------------------------------------------------------- function [h, numpeaks, threshold, nhood] = parseInputs(varargin) iptchecknargin(1,6,nargin,mfilename); h = varargin{1}; iptcheckinput(h, {'double'}, {'real', '2d', 'nonsparse', 'nonempty',... 'finite', 'integer'}, ... mfilename, 'H', 1); numpeaks = 1; % set default value for numpeaks idx = 2; if nargin > 1 if ~ischar(varargin{2}) numpeaks = varargin{2}; iptcheckinput(numpeaks, {'double'}, {'real', 'scalar', 'integer', ... 'positive', 'nonempty'}, mfilename, 'NUMPEAKS', 2); idx = 3; end end % Initialize to empty nhood = []; threshold = []; % Process parameter-value pairs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% validStrings = {'Threshold','NHoodSize'}; if nargin > idx-1 % we have parameter/value pairs done = false; while ~done input = varargin{idx}; inputStr = iptcheckstrs(input, validStrings,mfilename,'PARAM',idx); idx = idx+1; %advance index to point to the VAL portion of the input if idx > nargin eid = sprintf('Images:%s:valFor%sMissing', mfilename, inputStr); msg = sprintf('Parameter ''%s'' must be followed by a value.', inputStr); error(eid,'%s', msg); end switch inputStr case 'Threshold' threshold = varargin{idx}; iptcheckinput(threshold, {'double'}, {'real', 'scalar','nonnan' ... 'nonnegative'}, mfilename, inputStr, idx); case 'NHoodSize' nhood = varargin{idx}; iptcheckinput(nhood, {'double'}, {'real', 'vector', ... 'finite','integer','positive','odd'},... mfilename, inputStr, idx); if (any(size(nhood) ~= [1,2])) eid = sprintf('Images:%s:invalidNHoodSize', mfilename); msg = sprintf(['Value of parameter,''%s'', must be a two '... 'element row vector.'], inputStr); error(eid,'%s', msg); end if (any(nhood > size(h))) eid = sprintf('Images:%s:tooBigNHoodSize', mfilename); msg = sprintf(['Dimensions specified by ''%s'', '... 'must be smaller than '... 'size of Hough matrix H.'], inputStr); error(eid,'%s', msg); end otherwise eid = sprintf('Images:%s:internalError', mfilename); msg = 'Unknown input string.'; %should never get here error(eid,'%s', msg); end if idx >= nargin done = true; end idx=idx+1; end end % Set the defaults if necessary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if isempty(nhood) nhood = size(h)/50; nhood = max(2*ceil(nhood/2) + 1, 1); % Make sure the nhood size is odd. end if isempty(threshold) threshold = 0.5 * max(h(:)); end function lines = houghlines(varargin) %HOUGHLINES Extract line segments based on Hough transform. % LINES = HOUGHLINES(BW, THETA, RHO, PEAKS) extracts line segments % in the image BW associated with particular bins in a Hough % transform. THETA and RHO are vectors returned by function HOUGH. % Matrix PEAKS, which is returned by function HOUGHPEAKS, % contains the row and column coordinates of the Hough transform % bins to use in searching for line segments. HOUGHLINES returns % LINES structure array whose length equals the number of merged % line segments found. Each element of the structure array has % these fields: % % point1 End-point of the line segment; two-element vector % point2 End-point of the line segment; two-element vector % theta Angle (in degrees) of the Hough transform bin % rho Rho-axis position of the Hough transform bin % % The end-point vectors contain [X, Y] coordinates. % % LINES = HOUGHLINES(...,PARAM1,VAL1,PARAM2,VAL2) sets various % parameters. Parameter names can be abbreviated, and case % does not matter. Each string parameter is followed by a value % as indicated below: % % 'FillGap' Positive real scalar. % When HOUGHLINES finds two line segments associated % with the same Hough transform bin that are separated % by less than 'FillGap' distance, HOUGHLINES merges % them into a single line segment. % % Default: 20 % % 'MinLength' Positive real scalar. % Merged line segments shorter than 'MinLength' % are discarded. % % Default: 40 % % Class Support % ------------- % BW can be logical or numeric and it must be real, 2-D, and nonsparse. % % Example % ------- % Search for line segments corresponding to five peaks in the Hough % transform of the rotated circuit.tif image. Additionally, highlight % the longest segment. % % I = imread('circuit.tif'); % rotI = imrotate(I,33,'crop'); % BW = edge(rotI,'canny'); % [H,T,R] = hough(BW); % imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); % xlabel('\theta'), ylabel('\rho'); % axis on, axis normal, hold on; % P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); % x = T(P(:,2)); y = R(P(:,1)); % plot(x,y,'s','color','white'); % % % Find lines and plot them % lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7); % figure, imshow(rotI), hold on % max_len = 0; % for k = 1:length(lines) % xy = [lines(k).point1; lines(k).point2]; % plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % % % plot beginnings and ends of lines % plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); % plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); % % % determine the endpoints of the longest line segment % len = norm(lines(k).point1 - lines(k).point2); % if ( len > max_len) % max_len = len; % xy_long = xy; % end % end % % % highlight the longest line segment % plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan'); % % See also HOUGH and HOUGHPEAKS. % Copyright 1993-2003 The MathWorks, Inc. % $Revision: 1.1.8.2 $ $Date: 2005/12/12 23:20:08 $ % References: % Rafael C. Gonzalez, Richard E. Woods, Steven L. Eddins, "Digital % Image Processing Using MATLAB", Prentice Hall, 2003 [nonzeropix,theta,rho,peaks,fillgap,minlength] = parseInputs(varargin{:}); minlength_sq = minlength^2; fillgap_sq = fillgap^2; numlines = 0; lines = struct; for k = 1:size(peaks,1) % Get all pixels associated with Hough transform cell. [r, c] = houghpixels(nonzeropix, theta, rho, peaks(k,:)); if isempty(r) continue end % Compute distance^2 between the point pairs xy = [c r]; % x,y pairs in coordinate system with the origin at (1,1) diff_xy_sq = diff(xy,1,1).^2; dist_sq = sum(diff_xy_sq,2); % Find the gaps larger than the threshold. fillgap_idx = find(dist_sq > fillgap_sq); idx = [0; fillgap_idx; size(xy,1)]; for p = 1:length(idx) - 1 p1 = xy(idx(p) + 1,:); % offset by 1 to convert to 1 based index p2 = xy(idx(p + 1),:); % set the end (don't offset by one this time) linelength_sq = sum((p2-p1).^2); if linelength_sq >= minlength_sq numlines = numlines + 1; lines(numlines).point1 = p1; lines(numlines).point2 = p2; lines(numlines).theta = theta(peaks(k,2)); lines(numlines).rho = rho(peaks(k,1)); end end end %----------------------------------------------------------------------------- function [r, c] = houghpixels(nonzeropix, theta, rho, peak) %HOUGHPIXELS Compute image pixels belonging to Hough transform bin. % [R, C] = HOUGHPIXELS(NONZEROPIX, THETA, RHO, PEAK) computes the % row-column indices (R, C) for nonzero pixels NONZEROPIX that map % to a particular Hough transform bin, PEAK which is a two element % vector [RBIN CBIN]. RBIN and CBIN are scalars indicating the % row-column bin location in the Hough transform matrix returned by % function HOUGH. THETA and RHO are the second and third output % arguments from the HOUGH function. x = nonzeropix(:,1); y = nonzeropix(:,2); theta_c = theta(peak(2)) * pi / 180; rho_xy = x*cos(theta_c) + y*sin(theta_c); nrho = length(rho); slope = (nrho - 1)/(rho(end) - rho(1)); rho_bin_index = round(slope*(rho_xy - rho(1)) + 1); idx = find(rho_bin_index == peak(1)); r = y(idx) + 1; c = x(idx) + 1; %----------------------------------------------------------------------------- function [nonzeropix,theta,rho,peaks,fillgap,minlength] = ... parseInputs(varargin) iptchecknargin(1,8,nargin,mfilename); idx = 1; bw = varargin{idx}; iptcheckinput(bw, {'numeric','logical'},... {'real', '2d', 'nonsparse', 'nonempty'}, ... mfilename, 'BW', idx); idx = idx+1; theta = varargin{idx}; iptcheckinput(theta, {'double'}, {'real','vector','finite',... 'nonsparse','nonempty'}, ... mfilename, 'THETA', idx); idx = idx+1; rho = varargin{idx}; iptcheckinput(rho, {'double'}, {'real','vector','finite',... 'nonsparse','nonempty'}, ... mfilename, 'RHO', idx); idx = idx+1; peaks = varargin{idx}; iptcheckinput(peaks, {'double'}, {'real','2d','nonsparse','integer'}, ... mfilename, 'PEAKS', idx); if size(peaks,2) ~= 2 eid = sprintf('Images:%s:invalidPEAKS', mfilename); msg = sprintf('PEAKS must be a Q-by-2 matrix'); error(eid,'%s',msg); end % Set the defaults fillgap = 20; minlength = 40; % Process parameter-value pairs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% validStrings = {'FillGap','MinLength'}; idx = idx+1; if nargin > idx-1 % we have parameter/value pairs done = false; while ~done input = varargin{idx}; inputStr = iptcheckstrs(input, validStrings,mfilename,'PARAM',idx); idx = idx+1; %advance index to point to the VAL portion of the input if idx > nargin eid = sprintf('Images:%s:valFor%sMissing', mfilename, inputStr); msg = sprintf('Parameter ''%s'' must be followed by a value.', inputStr); error(eid,'%s',msg); end switch inputStr case 'FillGap' fillgap = varargin{idx}; iptcheckinput(fillgap, {'double'}, {'finite','real', 'scalar', ... 'positive'}, mfilename, inputStr, idx); case 'MinLength' minlength = varargin{idx}; iptcheckinput(minlength, {'double'}, {'finite','real', 'scalar', ... 'positive'}, mfilename, inputStr, idx); otherwise eid = sprintf('Images:%s:internalError', mfilename); msg = 'Unknown input string.'; %should never get here error(eid,'%s',msg); end if idx >= nargin done = true; end idx=idx+1; end end % Compute the required parameters [y, x] = find(bw); nonzeropix = [x, y] - 1; 简化整合为一个hough变换函数
12-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值